Narcissistic number 7-11 (20 minutes)

Refers to a number of N-bit daffodils positive integer (N≥3), on which digital bits of each of N power equals itself. For example: 153 = 13 + 5 + 3 3 3. This problem requires programming, calculates the number of bits for all N daffodils.

Input formats:

In a given input row positive integer N (3≤N≤7).

Output formats:

All N-bit output daffodils ascending order, each number per line.

Sample input:

3

Sample output:

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;
} 

 

Released six original articles · won praise 6 · views 237

Guess you like

Origin blog.csdn.net/qq_44461900/article/details/103995215