题解 | #自守数#

自守数

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

#include <iostream>
#include <string>
using namespace std;

bool isAuto(int n){
    long long square = static_cast<long long>(n) * n;
    string numStr = to_string(n); 
    string squareStr = to_string(square);
    return squareStr.substr(squareStr.length() - numStr.length()) == numStr;
}

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

int main() {
    int n;
    cin >> n;
    int result = countAuto(n);
    cout << result << endl;
}
// 64 位输出请用 printf("%lld")

转换为字符串,计算起始位,匹配字符串是否相同

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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