一、描述:
所谓的“水仙花数”是指一个三位数其各位数字的立方和等于该数本身。
例如153是“水仙花数”,因为:153 = 13 + 53 + 33。
在数论中,水仙花数(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。
二、分析:
对水仙花数的求法转化为对个位数、十位数、百位数的求法。
个位数求法:i%10
十位数求法:(i/10)%10
百位数求法:i/100
三、程序:
//求出100〜999之间的所有“水仙花数”并输出。
#include<stdio.h>
#include<windows.h>
#include<math.h>
#pragma warning(disable:4996)
int main(){
int i = 0;
int single=0, ten=0, hundred=0;
for (i=100;i < 1000; i++){
int single = i % 10; //个位数
int ten =( i / 10) % 10; //十位数
int hundred = i /100; //百位数
if (i == hundred* hundred* hundred + ten* ten*ten + single*single*single){
printf("%d\n", i);
}
}
system("pause");
return 0;
}