DFS
Problem Description
A DFS (digital factorial sum) number is found by summing the factorial of every digit of a positive integer.
For example , consider the positive integer 145 = 1!+4!+5!, so it's a DFS number.
Now you should find out all the DFS numbers in the range of int ( [1, 2147483647] ).
There is no input for this problem. Output all the DFS numbers in increasing order. The first 2 lines of the output are shown below.
翻译:一个数字的DFS(数字的阶乘总和)是通过对一个正整数的每个数字阶乘求和来找到的。
例如,考虑一下正整数145=1!+4!+5!,所以它是一个DFS数字。
现在你要找出所有的DFS数字在int([1,2147483647])范围内。
这个问题没有输入。按照递增的顺序输出所有的DFS数字。输出的前两行如下所示。
Input
no input
Output
Output all the DFS number in increasing order. (按照递增顺序输出所有的DFS数字)
Sample Output
1
2
......
考核知识点:
阶乘相关
常见误区:
1. 零的阶乘是1不是0。
2. 没有打阶乘表。
3. 没有考虑后面大量数字太大,其阶乘和很小(最大几百万左右),不可能等于本身。从而做了许多无用功而导致超时。
解题思路:
考虑到上面误区的部分,控制好需要计算的数的范围就能进行求解了。经过多次测试发现,输出的数字只有四个(附图)
那么大概可以肯定答案就是这四个数了。所以范围只要比40585大一点就可以了。
参考代码:
#include<cstdio>
using namespace std;
int main()
{
int a,b,c,ans,i,j,k;
int s[10];
s[0] = 1;
for(i = 1; i <= 9; i ++)
{
c = 1;
for(j = 1; j <= i; j ++)
{
c *= j;
}
s[i] = c;
}
for(i = 1; i <= 100000; i++)
{
a = i;
ans = 0;
while(a)
{
b = a%10;
a /= 10;
ans += s[b];
}
if(ans == i)
printf("%d\n",ans);
}
return 0;
总结:
在数据范围很大时候,一定要结合题意,思考是否可以减少不必要的运算,是否会做很多无用功。细节决定成败,在绝境中认真思考,会发现新的出路。