题解 | #自守数#
自守数
https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
#include <iostream> using namespace std; bool isO(int x) { int x2 = x * x; int x3 = x2 - x; bool res = false; if (x < 10) { if (x3 % 10 == 0) { res = true; } } else if (x >= 10 && x < 100) { if (x3 % 100 == 0) { res = true; } } else if (x >= 100 && x < 1000) { if (x3 % 1000 == 0) { res = true; } } else if (x >= 1000 && x < 10000) { if (x3 % 10000 == 0) { res = true; } } return res; } int main() { int n; cin >> n; int cnt = 1; for (int i = 1; i <= n ; i++) { if (isO(i)) { cnt++; } } cout << cnt; }
取余计算