-
[1389] The Sum of Factorial
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
-
对于一个数n,该数每位上的数的阶乘的总和如果等于n,那么该数是阶乘和数。
- 输入
-
输入包括一个数 N (1 <= N <= 18)。
- 输出
-
输出所有的 N 位数的阶乘和数。
如果没有答案,不用输出。
- 样例输入
-
1
- 样例输出
-
1 2
- 提示
-
无
- 来源
-
Hungar
这个题目还是比较好玩的,,就是没有写出来呢,,,刚开始没有什么思路呢,,,其实像这样的题目,其实符合的还是比较少的,,就不如,慢慢等他的结果,然后自己再打表
这样是比较正确的解法.0.-!
贴出代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
int p[11];
int main()
{
p[0] = 1;
p[1] = 1;
for (int i = 2; i <= 9; i++)
{
p[i] = i * p[i - 1];
// printf("p[%d] = %d \n" , i, p[i]);
/*
p[2] = 2
p[3] = 6
p[4] = 24
p[5] = 120
p[6] = 720
p[7] = 5040
p[8] = 40320
p[9] = 362880
*/
//由于8*p[9] = 2903040 << 10^7
//而 7 * p[9] = 2540160 > 10^6
}
for (int i = 1; i <= 10000000 ; i++)
{
int sum = 0;
int temp = i;
while (temp)
{
int t = temp % 10;
sum += p[t];
temp /= 10;
}
if (sum == i)
{
printf("%d\n",i);
}
}
/*
1
2
145
40585
请按任意键继续. . .
*/
//这就是运行结果.所以很容易得到运行结果.
//然后重新写一个程序就行了
system("pause");
return 0;
}