输出100—999中所有的水仙花数,若3位数xyz满足
, 则xyz为水仙花数,例如
, 因此153是水仙花数。
#include <iostream>
using namespace std;
// 方法一
void daffodil_1()
{
int a = 0;
for (int x=1; x<10; x++)
{
for (int y =0; y<10; y++)
{
for (int z = 0; z<10; z++)
{
a = 100*x+10*y+z;
if (a== x*x*x + y*y*y + z*z*z)
{
cout<<a<<" 是水仙花数"<<endl;
}
}
}
}
}
// 方法二
void daffodil_2()
{
int x = 0;
int y = 0;
int z = 0;
for (int n=100; n<1000; n++)
{
x = n / 100;
y = (n % 100)/10 ;
z = n % 10;
if(n == x*x*x + y*y*y +z*z*z)
{
cout<<n<<" 是水仙花数"<<endl;
}
}
}
int main()
{
cout<<"----------daffodil_1()---------"<<endl;
daffodil_1();
cout<<endl;
cout<<"----------daffodil_2()---------"<<endl;
daffodil_2();
return 0;
}
原文链接: https://www.cnblogs.com/changjl/p/5717121.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/237821
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!