创建一个程序,打印出前八个[8] automorphic numbers 。输出必须完成
在 main()
函数中。
这是我编写的代码:
#include <stdio.h>
#include <conio.h>
main() {
int automorphic[8];
int n;
printf("\t\t\tAUTOMORPHIC\n\n\n");
for(n=1; n<8; n++ ){
if (n*n%10==n || n*n%100==n || n*n%1000==n);
}
printf("\t%d\n\n", automorphic [n]);
getch();
return 0;
}
我不明白为什么它只打印出0?我的代码中是否缺少某些内容?或者我根本就没有做正确的事?
最佳答案
因为:
- 您仅检查数字 1 到 7(只有此范围内的数字 1、5 和 6 是自守的)。
- 您没有将数字存储在数组中。
- 您仅打印数组的第 7 个元素(始终为 0)。
- 您需要预期数字会比这个更大。
- 此外,自同数从 0 开始,而不是 1。
int main() {
long automorphic [8];
long i;
int n;
printf("\t\t\tAUTOMORPHIC\n\n\n");
i= 0 ;
n= 0 ;
while( n < 8 ) {
if( i*i%10==i || i*i%100==i || i*i%1000==i || i*i%10000==i || i*i%100000==i ) {
automorphic[n]= i ;
n++;
}
i++;
}
for(n= 0 ; n < 8 ; n++ ) {
printf("\t%d\n\n", automorphic[n] );
}
getch();
return 0;
}
结果应该是:
0
1
5
6
25
76
376
625
关于c - 打印出前八个自守数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18554173/