题解 | #自守数#

自守数

https://www.nowcoder.com/practice/88ddd31618f04514ae3a689e83f3ab8e

#include <stdio.h>
#include <string.h>
int main() {
    int n;
    scanf("%d", &n);
    int count = 1;
    for (int i = 1; i <= n; i++) {
        int ts[100];
        int c = 0;
        int m = i * i;
        while (m > 0) {
            ts[c] = m % 10;
            m = m / 10;
            c++;
        }
        /*        for (int j = 0; j < c; j++) {
                    printf("%d ", ts[j]);
                }*/
        int s1 = 0, s2 = 0, j = 1, k = 0, l;
        l = i;
        while (l > 0) {
            s1 += (l % 10) * j;
            s2 += ts[k] * j;
            l /= 10;
            j *= 10;
            k++;
        }
        if (s1 == s2)
            count++;
    }
    printf("%d\n", count);
    return 0;
}

全部评论

相关推荐

海康威视 软开岗 15k15
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务