Problem B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 488 Accepted Submission(s): 186
Problem Description
度熊面前有一个全是由1构成的字符串,被称为全1序列。你可以合并任意相邻的两个1,从而形成一个新的序列。对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列。
Input
这里包括多组测试数据,每组测试数据包含一个正整数
N
,代表全1序列的长度。
1≤N≤200
1≤N≤200
Output
对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量。
Sample Input
1 3 5
Sample Output
1 3 8Hint如果序列是:(111)。可以构造出如下三个新序列:(111), (21), (12)。
Source
Recommend
要加到第200个肯定超long long,此时要换高精度,网上有许多高精度的模板,但建议手写。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
string a[210];
string add(string str1,string str2)
{
int len1=str1.size();
int len2=str2.size();
if(len1<len2) {swap(str1,str2);swap(len1,len2);}
int c=0;
for(int i=len1-1,j=len2-1;i>=0;i--,j--)
{
c+=str1[i]-'0';
if(j>=0) c+=str2[j]-'0';
str1[i]=c%10+'0';
c/=10;
}
if(c)
str1='1'+str1;
return str1;
}
int main()
{
int n;
a[1]='1';
a[2]='2';
for(int i=3;i<205;i++)
a[i]=add(a[i-1],a[i-2]);
while(cin>>n)
cout<<a[n]<<endl;
}