在数论中,水仙花数(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<stdlib.h>
int main()
{
int num = 2;
int hundred = 0;
int ten = 0;
int unit = 0;
for (; num < 1000; num++)
{
hundred = num / 100;
ten = (num - hundred * 100) / 10;
unit = (num - hundred * 100 - ten * 10) / 1;
if (num == hundred * hundred * hundred
+ ten * ten * ten
+ unit *unit *unit)
{
printf("%d ", num);
}
}
printf("\n");
system("pause");
return 0;
}