题目链接:http://acm.hust.edu.cn/vjudge/contest/121397#problem/F
http://acm.hdu.edu.cn/showproblem.php?pid=1865
Description
You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’, or leave the ‘1’ there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find the total number of result you can get.
Input
The first line is a number n refers to the number of test cases. Then n lines follows, each line has a string made up of ‘1’ . The maximum length of the sequence is 200.
Output
The output contain n lines, each line output the number of result you can get .
Sample Input 3 1 11 11111 Sample Output 1 2 8
AC代码:
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 300 4 int str1[maxn][maxn]; 5 int main() 6 { 7 int T, i, j; 8 scanf("%d ",&T); 9 10 while(T--) 11 { 12 char str[300]; 13 gets(str); 14 15 int len=strlen(str); 16 str1[0][0]=1; 17 str1[1][0]=2; 18 19 for(i=2;i<len;i++) 20 { 21 int c=0,k; 22 for(k=0;k<maxn;k++) 23 { 24 int s=str1[i-1][k]+str1[i-2][k]+c; 25 str1[i][k]=s%10; 26 c=s/10; 27 } 28 while(c) 29 { 30 str1[i][k++]=c%10; 31 c/=10; 32 } 33 } 34 35 for(i=299;i>=0;i--) 36 if(str1[len-1][i]) 37 break; 38 39 for(j=i;j>=0;j--) 40 printf("%d",str1[len-1][j]); 41 printf("\n"); 42 } 43 return 0; 44 }
A完还A:
1 #include<stdio.h> 2 #include<string.h> 3 4 #define N 210 5 int f[1010][N]; 6 7 void init() 8 { 9 int i, j; 10 f[0][1] = 1; 11 f[1][1] = 1; 12 13 for(i = 2; i <= 1000; i++) 14 for(j = 1; j <= 200; j++) 15 { 16 f[i][j] += f[i-1][j] + f[i-2][j]; 17 if(f[i][j] >= 10) 18 { 19 f[i][j] -= 10; 20 f[i][j+1]++; 21 } 22 } 23 } 24 25 int main() 26 { 27 28 int i, j, t; 29 char s[N]; 30 31 scanf("%d", &t); 32 init(); 33 34 35 while(t--) 36 { 37 scanf("%s", s); 38 39 int n = strlen(s); 40 41 42 int k = 150; 43 for (; f[n][k] == 0 ; k--); 44 for (; k >= 1 ; k--) 45 printf ("%d",f[n][k]); 46 47 printf("\n"); 48 49 } 50 51 return 0; 52 }