7-11 水仙花数 (20分)

水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:153=1​3​​+5​3​​+3​3​​。 本题要求编写程序,计算所有N位水仙花数。

输入格式:

输入在一行中给出一个正整数N(3≤N≤7)。

输出格式:

按递增顺序输出所有N位水仙花数,每个数字占一行。

输入样例:

3

输出样例:

153
370
371
407
#include <bits/stdc++.h>
using namespace std;
int main()
{
//	freopen("D:\\LYJ.txt","r",stdin); 
	//不能用pow就循环
	int n,x=1,y=1;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		x*=10;
		y*=10;
	}  
	y*=10;
	for(int i=x;i<y;i++)//i为n为整数 
	{
		int t=i,sum=0;
		while(t!=0)
		{
			int m=n,tt=1,l=t%10;
			while(m--)
			{
				tt*=l;
			}
			sum+=tt;
			t/=10;
		}
		if(sum==i) cout<<i<<endl;
	}
	return 0;
} 
发布了6 篇原创文章 · 获赞 6 · 访问量 237

猜你喜欢

转载自blog.csdn.net/qq_44461900/article/details/103995215