题目1038:Sum of Factorials

View Code
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 
 6 int fac[11];
 7 int a[20],flag=0;
 8 int factorial(int n)
 9 {
10     if(n==0)
11         return 1;
12     else if(n==1)
13         return 1;
14     else
15         return factorial(n-1)*n;
16 }
17 void cal_fac()
18 {
19     for(int i=0;i<=10;++i)
20     {
21         fac[i]=factorial(i);
22         
23     }
24 }
25 
26 
27 
28 int main()
29 {
30     int n,i;
31     cal_fac();
32     while(cin>>n)
33     {
34         for(i=10;i>=0;--i)
35         {
36             if(fac[i]<=n)
37             {
38                 n-=fac[i];
39             }
40         }
41         if(n==0)
42             cout<<"YES"<<endl;
43         else
44             cout<<"NO"<<endl;
45     }
46     return 0;
47 
48 }

注意:此题需要仅需考虑0!-9!

posted @ 2013-01-20 20:38  dupuleng  阅读(98)  评论(0编辑  收藏  举报