admin管理员组

文章数量:1612139

Digit factorials

 

A curious number is equal to the sum of the factorials of its digits, for example 1! + 4! + 5! = 1 + 24 + 120 = 145, so 145 is a curiouse number. But 1! + 2! + 3! = 1 + 2 + 6 ≠ 123, so 123 is not a curiouse number.

 

Find the sum of all numbers which are less or equal to a given positve integer.

 

Note: 1! = 1 and 2! = 2 are not curious numbers.

 

Input

 

One    positive integer nn < 1000000.

 

Output

 

The first line is the sum of all curious numbers which are less or equal to n.

 

The second line is all the curious numbers which are ranged from small to big, and seperated with blank space. There is no space at the end of the line.

 

Sample Input

 

150

 

Sample Output

 

145

 

145

 

Hint

 

All curious numbers are positive.

 

题目大意:

        给你一个数,让你找到所有的数,这些数都具备一个共同的特征:各个数位的阶乘和等于它本身。

       如果只有一个符合要求的数,把这个数输出两遍;如果有很多个数,把所有的数一行一个输个遍。

       不能有空行!(真坑也真香)

代码:

      

#include<stdio.h>
int digit_factorial(int n);
int factorial(int n);
int factorial(int n){
	int sum = 1;
	for(int i = 1; i <= n ;i ++)
		sum *= i;
	return sum;
}
int digit_factorial(int n)
{
	int sum = 0;
	while(n)
	{
		sum += factorial(n%10);
		n /= 10;
	}
	return sum ;
	
}
int main()
{
	int n;
	scanf("%d",&n);int a[10];
	int  k = 0;
	for(int i = 3; i <= n ;i ++)
	{
		if(digit_factorial(i)==i)
		{
			a[k++] = i;		
		}
	}


			for(int i = 0 ;i < k ;i++)
			{
				printf("%d\n",a[i]);
			}
			printf("%d",a[k-1]);

	return 0;
}

 

本文标签: 奇数Digitfactorials