所谓水仙花数,是指一个n位数,其各位数字立方和等于该数本身。水仙花数是指一个n位数,它的每个位上的数字的立方和等于它本身。例如,153是一个水仙花数,为3位数,所以1^3 + 5^3 + 3^3 = 153。
具体代码如下:
打印0~100000之间的水仙花数
//打印水仙花数
int main()
{
int n = 0;
int a=0,b=0,c=0,d=0,e=0;
for (n = 0; n < 100000; n++)
{
e = n % 10;//个位
d = n / 10 % 10;//十位
c = n / 100 % 10;//百位
b = n / 1000 % 10;//千位
a = n / 10000;//万位
if (e == n)
{
printf("%d ", n);
}
else if (d * d + e * e == n)
{
printf("%d ", n);
}
else if (c*c*c+d*d*d+e*e*e==n)
{
printf("%d ", n);
}
else if (b*b*b*b+c*c*c*c+d*d*d*d+e*e*e*e==n)
{
printf("%d ", n);
}
else if (a*a*a*a*a+b*b*b*b*b+c*c*c*c*c+d*d*d*d*d+e*e*e*e*e==n)
{
printf("%d ", n);
}
}
return 0;
}
该代码灵活可变动,可根据自己的需求来设计自己需要的代码,以及想要解决的问题。