HDU 1028 递推,如果用递归的话会超时
View Code
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 6 int a[125][125]; 7 8 int main() 9 { 10 int n, i, j, k, num; 11 12 while( scanf("%d",&n) != EOF) 13 { 14 memset(a,0,sizeof(a)); 15 for(i=0; i<=n; i++) 16 { 17 a[i][0] = 0; 18 a[0][i] = 0; 19 a[i][1] = 1; 20 a[1][i] = 1; 21 } 22 for(i = 1; i <= n; i++) 23 for(j = 1; j <= n; j++) 24 { 25 if(i == j) a[i][j] = a[i][j-1]+1; 26 else if(i < j) a[i][j] = a[i][i]; 27 else a[i][j] = a[i][j-1] + a[i-j][j]; 28 } 29 30 printf("%d\n",a[n][n]); 31 } 32 return 0; 33 } 34 35