HJ99 题解 | #自守数#

自守数

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

#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

bool isZiShouShu(long n)

{

    if (n == 0 || n == 1) {

        return true;

    }

    long m = n * n;

    string str1 = std::to_string(n);

    string str2 = std::to_string(m);

    string str3 = str2.substr(str2.size() - str1.size());

    return str3 == str1;

}

int GetZiShouCount(long n)

{

    int count = 0;

    for (long i = 0; i <= n; i++) {

       if (isZiShouShu(i)) {

           count++;

       }

    }

    return count;

}

int main() {

    long n;

    while (cin >> n) { // 注意 while 处理多个 case

        cout << GetZiShouCount(n) << endl;

    }

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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