题目描述:
求出0〜999之间的所有“水仙花数”并输出。“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3?,则153是一个“水仙花数”。
/*
在数论中,水仙花数(Narcissistic number)也称为自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),是指一N位数,其各个数之N次方和等于该数。
例如153、370、371及407就是三位数的水仙花数,其各个数之立方和等于该数:
153 = 1^3 + 5^3 + 3^3。
370 = 3^3 + 7^3 + 0^3。
371 = 3^3 + 7^3 + 1^3。
407 = 4^3 + 0^3 + 7^3。
*/
#include<stdio.h>
#include<windows.h>
int main()
{
int count = 0;
printf("水仙花数:");
int n,i,j,t;
for (n = 0; n < 1000; n++){
i = n / 100;
j = n / 10 % 10;
t = n % 10;
if (n == i*i*i +j*j*j +t*t*t){
printf("%d\t", n);
count++;
}
}
printf("\n");
printf("水仙花个数:%d\n", count);
system("pause");
return 0;
}
结果显示
水仙花数:0 1 153 370 371 407
水仙花个数:6
请按任意键继续. . .