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
分析:斐波那契数列超过100的基本都是会炸内存(不是说100就会炸,但100到200之间的某个数会超出long long的可存范围),而这题算是大数加法的入门题吧——拿来入门时不错滴。
预处理大数加法代码(标答):
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char S[201][100];
int s2[100] = { 1 }, s1[100] = { 2 }, s3[100];
char A[201];
void AC()
{
memset(S, '\0', sizeof(S));
S[1][0] = '1';
S[2][0] = '2';
for (int i = 3;i<201;i++)
{
for (int j = 0;j<100;j++)
s3[j] = s1[j];
for (int j = 0;j<100;j++)
{
s1[j] += s2[j];
if (s1[j]>9)
{
s1[j] -= 10;
s1[j + 1]++;
}
}
int k = 0;
for (int l = 99, j = 0;l >= 0;l--)
{
if (k != 0 || s1[l]>0)
{
S[i][j++] = s1[l] + '0';
k++;
}
}
for (int l = 0;l<100;l++)
s2[l] = s3[l];
}
}
int main()
{
AC();
//printf("1\n");
/*for (int i = 0;i<201;i++)
printf("%d=%s\n", i, S[i]);
*/
int T;
scanf("%d", &T);
//getchar();
while (T--)
{
scanf("%s", &A);
printf("%s\n", S[strlen(A)]);
}
return 0;
}<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">//</span><span style="font-family: 宋体; background-color: rgb(255, 255, 255);">本代码由汶神特约赞助播出</span>
奇葩做法(强行装逼):
#include<cstdio>
#include<cstring>
int str0[1000], str1[1000], str[1000];
char a[210];
void bsum(int *str0, int *str1,int *str)
{
int len0,len1,i,j,s;
for (len0 = 0;str0[len0] != -1;len0++);
for (len1 = 0;str1[len1] != -1;len1++);
str[len1 + 1] = -1;
for (i=len1,len0--,len1--;len0>=0||len1>=0;len0--, len1--)
{
if (len0 >= 0)
{
s = str[len1+1]+str0[len0] + str1[len1];
str[len1 + 1] = s % 10;
str[len1] = s / 10;
}
else
str[len1 + 1] += str1[len1];
}
if (str[0] == 0)
for (j = 0;j <= i;j++)
str[j] = str[j + 1];
}
int main()
{
int i, j, n,len;
scanf("%d", &n);
while (n--)
{
memset(str0, 0, sizeof str0);
memset(str1, 0, sizeof str1);
str0[0] = 1, str1[0] = 2, str0[1] = -1, str1[1] = -1;
if (scanf("%s", a) == 1)
{
len = strlen(a);
for (i = 2;i < len;i++)
{
memset(str, 0, sizeof str);
bsum(str0, str1, str);
for (j = 0;str1[j] != -1;j++)
str0[j] = str1[j];
str0[j] = -1;
for (j = 0;str[j] != -1;j++)
str1[j] = str[j];
str1[j] = -1;
}
if (len == 1)
printf("%d\n", str0[0]);
else if (len == 2)
printf("%d\n", str1[0]);
else
{
for (i = 0;str[i] != -1;i++)
printf("%d", str[i]);
printf("\n");
}
}
}
return 0;
}