题目:
编程求出所有的“水仙花数”。所谓“水仙花数”是指一个3位数,
其中各位数字的立方和等于该数本身,例如153就是一个“水仙花数”,
因为153=1*1*1+5*5*5+3*3*3。
样例:
153是水仙花数
370是水仙花数
371是水仙花数
407是水仙花数
请按任意键继续. . .
代码如下:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
/**********Program**********/
int n,a[3],sum;
for(n=100;n<1000;n++)
{
a[0]=n/100;
a[1]=(n/10)%10;
a[2]=n%10;
sum=0;
for(int i=0;i<3;i++)
{
sum+=pow(double(a[i]),3);
}
if(sum==n)
cout<<n<<"是水仙花数"<<endl;
}
/********** End **********/
return 0;
}