题解 | #自守数#
自守数
https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
#include <iostream>
using namespace std;
//求自然数的位数
int digit(const int n) {
//如果是0,返回1
if (n <= 0)
return 1;
//如果是个位数,返回10
if (n < 10)
return 10;
//超过个位数的,递归取整
return 10 * digit(n / 10);
}
//判断自然数是否是自守数
bool judge(const int n) {
//如果是0,是自守数
if (n == 0)
return true;
//如果自然数正好与其平方的尾部相等,则是自守数;否则不是
int m = n * n;
if (m % digit(n) == n)
return true;
else return false;
}
//统计不大于n的自守数个数
int count(const int n) {
int num = 0;
for (int i = 0; i <= n; i++) {
if (judge(i))
num++;
}
return num;
}
int main() {
int n;
while (cin >> n) { // 注意 while 处理多个 case
cout << count(n) << endl;
}
}
// 64 位输出请用 printf("%lld")
查看14道真题和解析
曼迪匹艾公司福利 129人发布