求出0~999之间的所有“水仙花数”并输出。
“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=13+53+3^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>
int main()
{
int i;
printf("求出0~999之间的所有“水仙花数”并输出:\n");
for(i=100;i<=999;i++)
{
int unitPlace = i / 1 % 10;
int tenPlace = i / 10 % 10;
int hundredPlace = i / 100 % 10;
// /是除,如果是整数相除,只取整数部分,没有四舍五入之类的
// %是取模,即取除法的余数
if(i==unitPlace*unitPlace*unitPlace+tenPlace*tenPlace*tenPlace+hundredPlace*hundredPlace*hundredPlace)
{
printf("%d\n",i);
}
}
return 0;
}