输出三位数的所有的“水仙花数”,所谓的“水仙花数”是指一个三位数其各位数字的立方和等于该数本身,例如153是“水仙花数”,因为:153 = 1^3+ 5^3 + 3^3;
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int a=0,b=0,c=0;//分别用于保存三位数的百位,十位,个位
int x=0;//用于保存水仙花数
cout<<"水仙花数为:"<<endl;
for(int n=100;n<1000;++n)
{
a=n%1000/100;//求百位
b=n%100/10;//求十位
c=n%10;//求个位
x=pow(a,3)+pow(b,3)+pow(c,3);
if(n==x)
{
cout<<n<<endl;
}
}
return 0;
}