在数论中,水仙花数(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。
显然0,1也是“水仙花数”,这里我们只讨论三位数的情况。
#include <stdio.h>
#include <math.h>
int main()
{
printf("0~999之间的所有“水仙花数”有:\n");
for (int i = 100; i < 1000; i++)
{
if (i == pow(i / 100, 3) + pow(i/10%10, 3) + pow(i%100%10, 3))
printf("%d\n", i);
}
system("pause");
return 0;
}