编程求出所有的水仙花数。
所谓水仙花数,是指一个三位正整数,其各位数字的立方和等于该整数本身。
如:1*1*1+5*5*5+3*3*3=153。
问题: 编程求出所有的水仙花数。各个数之间用空格隔开。
示例:153 370 371 407
代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a=100,s,d,f;
for(a=100;a<1000;a++)
{
s=a%10;//求出个位
d=(a/10)%10;//求出十位
f=a/100;//求出百位
if(a==s*s*s+d*d*d+f*f*f)
cout<<a<<" ";
}
cout<<endl;
return 0;
}