1、打表问题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //1364495 2009-05-13 20:42:44 Time Limit Exceeded 2212 2000MS 232K 426 B C++ Wpl 2 3 //1364655 2009-05-13 21:03:44 Accepted 2212 0MS 204K 299 B C++ Wpl 4 5 /*For example ,consider the positive integer 145 = 1!+4!+5!, so it's a DFS number. 6 7 Now you should find out all the DFS numbers in the range of int( [1, 2147483647] ).*/ 8 9 #include <iostream> 10 11 #define MAX 5 12 13 using namespace std; 14 15 int data[MAX]; 16 17 int main() 18 19 { 20 21 data[0]=1; 22 23 data[1]=2; 24 25 data[2]=145; 26 27 data[3]=40585; 28 29 int i; 30 31 for(i=0;i<=3;i++) 32 33 printf("%d ",data[i]); 34 35 return 0; 36 37 }
2、记忆数组(省去不必要的操作)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //1364495 2009-05-13 20:42:44 Time Limit Exceeded 2212 2000MS 232K 426 B C++ Wpl 2 3 #include <iostream> 4 5 #include <fstream> 6 7 #define MAX 10000 8 9 using namespace std; 10 11 int f[11],data[MAX]; 12 13 bool DFS(int n) 14 15 { 16 17 int sum=0,x=n; 18 19 while(x!=0) 20 21 { 22 23 sum+=f[x%10]; 24 25 x=x/10; 26 27 if(sum>n) 28 29 return false; 30 31 } 32 33 if(sum==n) 34 35 return true; 36 37 else 38 39 return false; 40 41 } 42 43 int main() 44 45 { 46 47 int i,j; 48 49 f[0]=1; 50 51 for(i=1;i<=10;i++) 52 53 f[i]=i*f[i-1]; 54 55 ofstream outfile("ans.txt"); 56 57 j=0; 58 59 for(i=1;i<2147483647;i++) 60 61 { 62 63 if(DFS(i)) 64 65 { 66 67 outfile<<"data["<<j++<<"]="<<i<<endl; 68 69 } 70 71 } 72 73 return 0; 74 75 }
3、如果超时,可以尝试后面的不用输出,将结果找出之后打表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 bool DFS(int x); 5 int jie(int n) 6 { 7 if(n == 1 || n == 0) return 1; 8 return n * jie(n-1); 9 } 10 int main() 11 { 12 int i; 13 for(i = 1; i <= 40585; i++)//2147483647 14 if(DFS(i)) 15 cout << i << endl; 16 return 0; 17 } 18 bool DFS(int x) 19 { 20 int xx = x, sum = 0; 21 while(x != 0) 22 { 23 int tmp = x % 10; 24 sum += jie(tmp); 25 x /= 10; 26 if(sum > xx) 27 return false; 28 } 29 if(xx == sum) 30 return true; 31 return false; 32 }