题解 | #自守数#
自守数
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")
转换为字符串,计算起始位,匹配字符串是否相同