题解 | #自守数#

自守数

https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e

#include <iostream>
using namespace std;

bool isO(int x) {
    int x2 = x * x;
    int x3 = x2 - x;
    bool res = false;
    if (x < 10) {
        if (x3 % 10 == 0) {
            res = true;
        }
    } else if (x >= 10 && x < 100) {
        if (x3 % 100 == 0) {
            res = true;
        }

    } else if (x >= 100 && x < 1000) {
        if (x3 % 1000 == 0) {
            res = true;
        }

    } else if (x >= 1000 && x < 10000) {
        if (x3 % 10000 == 0) {
            res = true;
        }
    }
    return res;

}

int main() {
    int n;
    cin >> n;
    int cnt = 1;
    for (int i = 1; i <= n ; i++) {
        if (isO(i)) {
            cnt++;
        }
    }
    cout << cnt;
}

取余计算

全部评论

相关推荐

烤点老白薯:他第二句话的潜台词是想让你帮他点个瑞幸或者喜茶啥的
mt对你说过最有启发的一...
点赞 评论 收藏
分享
牛至超人:把哈工大,再加大加粗,看见闪闪发光的哈工大字样,面试官直接流口水
投递字节跳动等公司6个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务