zoukankan      html  css  js  c++  java
  • 1sting

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
    题意: 给出以1组成的字符串,计算邻近的1组成2的方式有多少种。
     
    分析: 首先随意枚举几个字符串1,11,111,1111,很快发现他们的组成方式种数分别是1,2,3,5,在继续类推下去,发现其实就是斐波那契数列。
        但是题目告诉最长有200个1,用longlong根本无法存下,于是采用字符串处理。
     1 #include<cstring>
     2 #include<cstdio>
     3 #include<iostream>
     4 
     5 
     6 using namespace std;
     7 
     8 char s[205][2000]; //字符数组,存储两百个大数
     9 
    10 void ADD()
    11 {
    12     strcpy(s[1],"1");
    13     strcpy(s[2],"2"); //赋初值
    14     for(int k=3;k<=200;k++)
    15     {
    16         char c[2000];//倒序 储存当前结果
    17         int p=0,l=0;
    18         for(int i=strlen(s[k-1])-1,j=strlen(s[k-2])-1;i>=0||j>=0;i--,j--) //取末尾字符 向前相加
    19         {
    20             if(i>=0&&j>=0) c[l]=s[k-1][i]+s[k-2][j]-'0'+p;
    21             else if(i>=0) c[l]=s[k-1][i]+p;
    22             else if(j>=0) c[l]=s[k-2][j]+p;
    23                       //字符形式存储
    24             if(c[l]>'9') p=1,c[l]-=10;
    25             else p=0;
    26             l++;
    27         }
    28         if(p) c[l++]='1';
    29         c[l]='';
    30 
    31         for(int i=0,j=strlen(c)-1;i<j;i++,j--) {char a;a=c[i];c[i]=c[j];c[j]=a;} //将数组c倒过来 表示当前相加结果
    32         strcpy(s[k],c); //存储到结果s中
    33 
    34     }
    35 }
    36 
    37 int main()
    38 {
    39     ADD();//打表
    40     int T;
    41     cin>>T;
    42     while(T--)
    43     {
    44         char str[205];
    45         cin>>str;
    46         int n=strlen(str);
    47         cout<<s[n]<<endl;
    48     }
    49 }
  • 相关阅读:
    Dubbo服务的搭建
    实现类似AOP的封装和配置
    Java中的代理--proxy
    Java中的类加载器--Class loader
    Dubbo框架的说明
    Java中的泛型--generic
    git回退单个文件
    shell重定向的顺序问题
    Shell基本正则表达式和扩展正则表达式
    cgroup & oom-killer 简介
  • 原文地址:https://www.cnblogs.com/wsaaaaa/p/4287841.html
Copyright © 2011-2022 走看看