加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

E - 1sting (递推+大数加法)

发布时间:2020-12-14 02:49:51 所属栏目:大数据 来源:网络整理
导读:根据题意递推构造类fibnacci数列,由于要算到第200项,已经远远超出long long 所能表示的范围。 要用大数加法。即用数组模拟。 Description You will be given a string which only contains ‘1’; You can merge two adjacent ‘1’ to be ‘2’,or leave t

根据题意递推构造类fibnacci数列,由于要算到第200项,已经远远超出long long 所能表示的范围。

要用大数加法。即用数组模拟。


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 22222
?
Sample Output

1 2 8
?

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

typedef long long ll;
char s[210];
char f[210][210];
char s1[210],s2[210];

void init()
{
    int l1,l2;
    memset(f,sizeof(f));
    strcpy(f[1],"1");
    strcpy(f[2],"2");
    strcpy(f[3],"3");
    for(int k=4;k<=200;k++)
    {
        strcpy(s1,f[k-1]);
        strcpy(s2,f[k-2]);
        l1 = strlen(s1);
        l2 = strlen(s2);
        int i=0,c = 0,x = 0;
        while(i<l1 && i<l2)
        {
            int t = s1[i]-'0'+s2[i]-'0'+c;
            if(t>=10)
            {
                c = t/10;    //这里不是简单的c=1
                f[k][x++] = t%10+'0';
            }
            else
            {
                f[k][x++] = t+'0';
                c = 0;     //这里忘记清0
            }
            i++;
        }
        while(i<l1)
        {
            if(c)
            {
                int t = (s1[i]-'0'+c);
                f[k][x++] = t%10+'0';
                i++;
                c = t/10;
            }
            else
                f[k][x++] = s1[i++];
        }
        while(i<l2)
        {
            if(c)
            {
                int t = (s2[i]-'0'+c);
                f[k][x++] = t%10+'0';
                i++;
                c = t/10;
            }
            else f[k][x++] = s2[i++];
        }
        if(c)
            f[k][x++] = c+'0';
    }
}

int main()
{
    int T;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        int len = strlen(s);
        int p = strlen(f[len]);
        for(int i=p-1;i>=0;i--)
            printf("%c",f[len][i]);
        printf("n");
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读