Digit factorials(好奇数)

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值