hdu1865 1sting (递归+大数加法) 1sting Problem B

hdu1865 1sting (递归+大数加法)
1sting
Problem B

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6878    Accepted Submission(s): 2678


Problem 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
 
输出的结果巨长,远超出一般的数据范围^v^
 
hdu1865 1sting (递归+大数加法)
1sting
Problem B
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int a[1010][1010];//因为是大数,一维数组并不能储存数字,所以用二维,后面用来分别储存大数的每个位数
int main()
{
    int n,t;
    int r,s,i;
    r=0;
    memset(a,0,sizeof(a));
    a[1][1]=1;
    a[2][1]=2;
    for(i=1;i<=997;i++)//大数的加法
    {
        for(int j=1;j<=1010;j++)//底位在数组前面,高位存在数组后面
        {
            s=a[i][j]+a[i+1][j]+r;
            a[i+2][j]=s%10;
            r=s/10;
        }
    }
    scanf("%d",&t);
    while(t--)
    {
        char str[201];
        cin>>str;
        n=strlen(str);
        if(n==1)
          printf("1
");
        else if(n==2)
          printf("2
");
        else 
        {
            for(i=1010;i>=1;i--)//从高位开始输出
              if(a[n][i])
                break; //当a[n][i]为0时跳出,此时的i即答案有几位数,进入下一步的输出
            for(;i>=1;i--)  
              printf("%d",a[n][i]);  
            printf("
");  
        }
    }
    return 0;
}

相同的题目:hdu5686

Problem B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1615    Accepted Submission(s): 665


Problem Description
  度熊面前有一个全是由1构成的字符串,被称为全1序列。你可以合并任意相邻的两个1,从而形成一个新的序列。对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列。
 
Input
这里包括多组测试数据,每组测试数据包含一个正整数200
 
Output
对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量。
 
Sample Input
1
3
5
 
Sample Output
1
3
8
Hint
如果序列是:(111)。可以构造出如下三个新序列:(111), (21), (12)。
 
 
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int a[1010][1010];//因为是大数,一维数组并不能储存数字,所以用二维,后面用来分别储存大数的每个位数
int main()
{
    int n;
    int r,s,i;
    r=0;
    memset(a,0,sizeof(a));
    a[1][1]=1;
    a[2][1]=2;
    for(i=1;i<=997;i++)//大数的加法
    {
        for(int j=1;j<=1010;j++)//底位在数组前面,高位存在数组后面
        {
            s=a[i][j]+a[i+1][j]+r;
            a[i+2][j]=s%10;
            r=s/10;
        }
    }
    while( cin>>n)
    {
        if(n==1)
          printf("1
");
        else if(n==2)
          printf("2
");
        else 
        {
            for(i=1010;i>=1;i--)//从高位开始输出
              if(a[n][i])
                break; //当a[n][i]为0时跳出,此时的i即答案有几位数,进入下一步的输出
            for(;i>=1;i--)  
              printf("%d",a[n][i]);  
            printf("
");  
        }
    }
    return 0;
}