I00038 自守数(Automorphic number)

如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数(Automorphic number),参见百度百科_自守,或参见维基百科的Automorphic number自守数在OEIS中的数列号为A003226

问题描述:输入n(n为int类型,计算输出n的所有自守数,每个数之间用空格隔开,输出在一行里。

问题分析:解决该问题有暴力法brute force method和数学的方法,暴力法对于初学编程的人来说相对比较简单,而用数学的方法来解决可以再计算时间上得到大幅改善。暴力法也成为枚举法或穷举法(Proof by exhaustion)。这里给出的程序是使用暴力法的解法。

程序说明:(略)。

AC的C语言程序如下:

/* I00038 自守数(Automorphic number) */

#include <stdio.h>

int main(void)
{
    unsigned int n, d1, d2, i, temp;
    unsigned long long square;

    scanf("%d", &n);

    d1 = d2 = 0;
    for(i=0; i<=n; i++) {
        square = (long long)i * i;
        temp = i;
        while(temp) {
            d1 = temp % 10;
            d2 = square % 10;
            if(d1 != d2)
                break;
            temp /= 10;
            square /= 10;
        }
        if(d1 == d2)
            printf("%d ", i);
    }
    printf("
");

    return 0;
}

运行实例:

999999
0 1 5 6 25 76 376 625 9376 90625 109376 890625

原文地址:https://www.cnblogs.com/tigerisland/p/7564319.html