Exercise 4-6 Number of Daffodils (20 marks)

Exercise 4-6 Number of daffodils (20 points) The
number of daffodils refers to an N-digit positive integer (N≥3), and the sum of the N-th power of the numbers in each digit is equal to itself. E.g:Insert picture description here

This question requires writing a program to calculate the number of all N-digit daffodils.

Input format:
Input a positive integer N (3≤N≤7) in one line.

Output format:
output all N-digit daffodil numbers in increasing order, each number occupies one line.

Input sample:
3
Output sample:
153
370
371
407

#include<stdio.h>
int main()
{
    
    
	int x = 1 , y = 1;
	int  n;
	scanf("%d",&n);
	for(int i = 1 ; i < n ;i ++)
	{
    
    
		x *= 10;
		y *= 10;
	}
	y *= 10;
	for(int i = x ;i < y ;i ++)
	{
    
    
		int t = i,sum = 0;
		while(t != 0)
		{
    
    
			int m = n,tt = 1, l = t%10;
			while(m--)
			{
    
    
				tt*=l;
				
			}
			sum += tt;
			
			t /= 10;
			//
		}
		//printf("sum = %d\n",sum);
		if(sum==i)
			printf("%d\n",i);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/ChaoYue_miku/article/details/115221610