题解 | #自守数#纯C
自守数
https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e
#include <stdio.h> #include <string.h> int main() { int num; scanf("%d", &num); int count = 0; for (int i = 0; i <= num; i++) { int ret = i * i; char str_num[6], str_ret[20]; sprintf(str_num, "%d", i); //类型转换,将数字转换为字符串以便进行比较 sprintf(str_ret, "%d", ret); int len_num = strlen(str_num); int len_ret = strlen(str_ret); int tem = 0; for (int j = 0; j < len_num; j++) { if (str_num[j] == str_ret[len_ret - len_num + j]) { tem++; } } if (tem == len_num) { count++; } } printf("%d", count); return 0; }