春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的: “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。 现在要求输出所有在m和n范围内的水仙花数。
求10000以内的水仙花数
#include<stdio.h>
#include<math.h>
int main()
{
int i=0;
for(i=0;i<=10000;i++)
{
int n=1;
int temp=i;
int sum=0;
//先判断有多少位
while(temp/10)
{
n++;
temp=temp/10;
}
//再计算他的n次方之和
temp=i;
while(temp)
{
sum=sum+pow(temp%10,n);
temp=temp/10;
}
if(sum==i)
{
printf("%d ",i);
}
}
return 0;
}