zoukankan      html  css  js  c++  java
  • 度熊全是由1构成的字符串

      度熊面前有一个全是由1构成的字符串,被称为全1序列。你可以合并任意相邻的两个1,从而形成一个新的序列。对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列。

    Input

    这里包括多组测试数据,每组测试数据包含一个正整数N ,代表全1序列的长度。

    1N200

    Output

    对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量。

    Sample Input

    1
    3
    5

    Sample Output

    1
    3
    8
    
    
            
     

    Hint

    如果序列是:(111)。可以构造出如下三个新序列:(111), (21), (12)。

    高精度斐波那契数列 超出long long
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 string s[201];
     6 string add(string a,string b)
     7 {
     8     string ans="";
     9     int aa[1000]={0},bb[1000]={0};
    10     int aLen=a.size();
    11     int bLen=b.size();
    12     int MaxLen=max(aLen,bLen);
    13     for(int i=0;i<aLen;i++)
    14         aa[aLen-i-1]=a[i]-'0';
    15     for(int i=0;i<bLen;i++)
    16         bb[bLen-1-i]=b[i]-'0';
    17     for(int i=0;i<MaxLen;i++){
    18         aa[i]+=bb[i];
    19         aa[i+1]+=aa[i]/10;
    20         aa[i]%=10;
    21     }
    22     if(aa[MaxLen]) MaxLen++;
    23     for(int i=MaxLen-1;i>=0;i--)
    24         ans+=aa[i]+'0';
    25     return ans;
    26 }
    27 int main()
    28 {
    29     int n;
    30     s[0]="1",s[1]="1";
    31     for(int i=2;i<201;i++){
    32         s[i]=add(s[i-1],s[i-2]);
    33     }
    34     while(cin>>n){
    35         cout<<s[n]<<endl;
    36     }
    37     return 0;
    38 }
      1 #include<stdio.h>
      2 #include<string>
      3 #include<string.h>
      4 #include<iostream>
      5 #include<bits/stdc++.h>
      6 using namespace std;
      7 #define maxn 205
      8 //compare比较函数:相等返回0,大于返回1,小于返回-1
      9 int compare(string str1,string str2)
     10 {
     11     if(str1.length()>str2.length()) return 1;
     12     else if(str1.length()<str2.length())  return -1;
     13     else return str1.compare(str2);
     14 }
     15 //高精度加法
     16 //只能是两个正数相加
     17 string add(string str1,string str2)//高精度加法
     18 {
     19     string str;
     20     int len1=str1.length();
     21     int len2=str2.length();
     22     //前面补0,弄成长度相同
     23     if(len1<len2)
     24     {
     25         for(int i=1;i<=len2-len1;i++)
     26            str1="0"+str1;
     27     }
     28     else
     29     {
     30         for(int i=1;i<=len1-len2;i++)
     31            str2="0"+str2;
     32     }
     33     len1=str1.length();
     34     int cf=0;
     35     int temp;
     36     for(int i=len1-1;i>=0;i--)
     37     {
     38         temp=str1[i]-'0'+str2[i]-'0'+cf;
     39         cf=temp/10;
     40         temp%=10;
     41         str=char(temp+'0')+str;
     42     }
     43     if(cf!=0)  str=char(cf+'0')+str;
     44     return str;
     45 }
     46 //高精度减法
     47 //只能是两个正数相减,而且要大减小
     48 string sub(string str1,string str2)//高精度减法
     49 {
     50     string str;
     51     int tmp=str1.length()-str2.length();
     52     int cf=0;
     53     for(int i=str2.length()-1;i>=0;i--)
     54     {
     55         if(str1[tmp+i]<str2[i]+cf)
     56         {
     57             str=char(str1[tmp+i]-str2[i]-cf+'0'+10)+str;
     58             cf=1;
     59         }
     60         else
     61         {
     62             str=char(str1[tmp+i]-str2[i]-cf+'0')+str;
     63             cf=0;
     64         }
     65     }
     66     for(int i=tmp-1;i>=0;i--)
     67     {
     68         if(str1[i]-cf>='0')
     69         {
     70             str=char(str1[i]-cf)+str;
     71             cf=0;
     72         }
     73         else
     74         {
     75             str=char(str1[i]-cf+10)+str;
     76             cf=1;
     77         }
     78     }
     79     str.erase(0,str.find_first_not_of('0'));//去除结果中多余的前导0
     80     return str;
     81 }
     82 //高精度乘法
     83 //只能是两个正数相乘
     84 string mul(string str1,string str2)
     85 {
     86     string str;
     87     int len1=str1.length();
     88     int len2=str2.length();
     89     string tempstr;
     90     for(int i=len2-1;i>=0;i--)
     91     {
     92         tempstr="";
     93         int temp=str2[i]-'0';
     94         int t=0;
     95         int cf=0;
     96         if(temp!=0)
     97         {
     98             for(int j=1;j<=len2-1-i;j++)
     99               tempstr+="0";
    100             for(int j=len1-1;j>=0;j--)
    101             {
    102                 t=(temp*(str1[j]-'0')+cf)%10;
    103                 cf=(temp*(str1[j]-'0')+cf)/10;
    104                 tempstr=char(t+'0')+tempstr;
    105             }
    106             if(cf!=0) tempstr=char(cf+'0')+tempstr;
    107         }
    108         str=add(str,tempstr);
    109     }
    110     str.erase(0,str.find_first_not_of('0'));
    111     return str;
    112 }
    113 
    114 
    115 string fib[maxn];
    116 void Fib(){
    117     fib[0] = "0";
    118     fib[1] = "1";
    119     for(int i = 2;i < maxn; i++)
    120         fib[i] = add(fib[i - 1], fib[i - 2]);
    121 }
    122 
    123 int N;
    124 int main() {
    125     Fib();
    126     while(~scanf("%d", &N)){
    127         cout << fib[N + 1] << endl;
    128     }
    129     return 0;
    130 }
  • 相关阅读:
    【NOIP 2003】 加分二叉树
    【POJ 1655】 Balancing Act
    【HDU 3613】Best Reward
    【POJ 3461】 Oulipo
    【POJ 2752】 Seek the Name, Seek the Fame
    【POJ 1961】 Period
    【POJ 2406】 Power Strings
    BZOJ3028 食物(生成函数)
    BZOJ5372 PKUSC2018神仙的游戏(NTT)
    BZOJ4836 二元运算(分治FFT)
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6590703.html
Copyright © 2011-2022 走看看