转换成字符串去比较
#include <iostream> using namespace std; bool is_automorph(int n) { if(n == 0) return true; long square = n * n; string s1 = to_string(n); string s2 = to_string(square); int idx1 = s1.size() - 1, idx2 = s2.size() - 1; while(idx1 >= 0) { if(s1[idx1--] != s2[idx2--]) return false; } return true; } int main() { int count = 0; int n; cin >> n; for(int i = 0; i <= n; ++i) { if(is_automorph(i)) ++count; } cout << count << endl; return 0; }