前提:明确“水仙花数”的含义
水仙花数:在数论中,水仙花数(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<math.h>
int main()
{
int i = 0,sum=0;
for (i = 100; i <= 999; i++)
{
int a = i % 10;//个位
int b = (i / 10) % 10;//十位
intc = i / 100;//百位
sum = pow(a, 3) + pow(b, 3) + pow(c, 3);
if (sum == i)
{
printf("%d ", sum);
}
}
return 0;
}
**难点:**pow函数的使用
拓展: pow() 函数的作用是求一个数的n次方;例如3的平方为pow(3,2);
//头文件#include<math.h>.