题解 | #自守数#

自守数

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

#include <bits/stdc++.h>
using namespace std;
// 判断参数 n 是否是完全数
// 如果 n 是完全数则返回 true,否则返回false。
bool fun(int n) {
    // 计算平方数 m => 25 * 25 => 625
    int m = n * n;
    // l 用来算出正好大于 n 的基数,这里n是25那么y就是100
    int l = 10;
    while (l < n) {
        l *= 10;
    }
    // m 625 % y 100 = 25
    return m % l == n;
}

int counter(int n) {
    int count = 0;
    for (int i = 0;i <= n;i++) {
        if (fun(i)) {
            count++;
        }
    }
    return count;
}

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        cout << counter(n) << endl;
    }
}

全部评论

相关推荐

07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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