c++找100-999之间的水仙花数

原问题:c++找100-999之间的水仙花数
分类:编程开发 > 最后更新时间:【2017-07-27 02:19:36】

最佳答案

三位的很简单,用以下程序就可以了

#include <iostream>
#include<math.h>
using namespace std;

int main(void)
{
for(int i = 100; i <= 999; i++)
{
int a=i%10;
int b=i/10%10;
int c=i/100;
if (i==a*a*a+b*b*b+c*c*c)
cout << i << endl;
}
return 0;
}
多位的,可用以下函数

int Is_Narcissistic(int n) //是否水仙花数
{
int i,m,ct,b[20];
if (n<100) //3位以下没有
return 0;
m=n;
ct=0;
while(m!=0)
{
b[ct++]=m%10;
m/=10;
}
for(i=0;i<ct;i++)
m+=(int)pow(b[i],ct);
if (m==n)
return 1;
return 0;
}

最佳答案由网友  whoami1978  提供
公告: 为响应国家净网行动,部分内容已经删除,感谢网友理解。
17

分享到:

其他回答

其它网友回答:
#include"iostream.h"

其它网友回答:
void main()

其它网友回答:
{

其它网友回答:
 int i,j,k,n;

其它网友回答:
for(n=100;n<=999;n++)

其它网友回答:
{  

其它网友回答:
 i=n/100;   

其它网友回答:
j=n/10%10;  

其它网友回答:
 k=n%10;  

其它网友回答:
 if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)

其它网友回答:
 cout<<n<<endl;}

其它网友回答:
}

其它网友回答:
100-999=-899

    推荐