Experiment 4-2-5 Number of daffodils (very short time-consuming)

The number of daffodils refers to an N-digit positive integer (N≥3), and the sum of the N-th power of the numbers on each digit is equal to itself. For example: 153=1​3​​+5​3​​+3​3​​. This question requires writing a program to calculate the number of all N-digit daffodils.

Input format:

Enter 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

Sample output:

153
370
371
407

This question may time out according to common practice. It is not recommended to use the pow function in the math function. It can be solved by customizing the pow function. On the basis of passing, some small methods can be used to shorten the time-consuming

AC code

#include<stdio.h>
int pow(int a,int n)
{
    
    
 int result=1;
 for(int i=1;i<=n;i++)
  result*=a;
 return result;
}
int main() 
{
    
    
 	int n,num=1;
 	int p[10];
 	scanf("%d",&n);
 	for(int i=0;i<10;i++)
        	p[i]=pow(i,n);
 	num=pow(10,n-1);
 	for(int i=num;i<num*10;i++)
 	{
    
    
 	 	int j=i,sum=0;
  		while(j)
  		{
    
    
   			sum+=p[j%10];
   			j/=10;
  		}
  		if(sum==i)
   			printf("%d\n",i);
 	} 
}

Guess you like

Origin blog.csdn.net/weixin_45989486/article/details/106032519