首页 > 代码库 > 1410121949-hd-1sting

1410121949-hd-1sting

1sting

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3442    Accepted Submission(s): 1332


Problem 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’。当然,你可能会得到很多不同的结果。例如,你可以得到1111,1111,121,112211,22。现在你的工作是找到这个字符串的结果最多种类数。

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.

第一行是一个数n指的是测试用例的数目。然后N线以下,每行有一个由1个字符串。序列的最大长度为200

错误原因

       一开始发现了这是斐波那契数列,也想到了用int64位,但是范围仍然不够,好吧,原谅我没想到会和大数加减结合。大数加减这一块自己也掌握的不好。还有用if之后,会习惯性忘记else这一情况。

代码

#include<stdio.h>
#include<string.h>
char s[230];
int num[230][1000];
int len[1000];
//int len[100];  数组开小了。 
int main()
{
	//这道题是斐波那契数列和大数加减法的结合 
	int n;
	int i,j,k,l,m;
	memset(num,0,sizeof(num));
	//二维数组也可以这样初始化。 
	num[1][0]=1;
	num[2][0]=2;
	len[1]=len[2]=1;
	l=0;
	for(i=3;i<210;i++)
	{
		k=0;
		for(j=0;j<=len[i-1];j++)
		{
			l=k+num[i-1][j]+num[i-2][j];
			num[i][j]=l%10;
			k=l/10;
		}
	//需要定义l,k两个变量。 
		if(num[i][len[i-1]]!=0)
		    len[i]=len[i-1]+1;
		else//else老是忘记,这儿出错了 
		    len[i]=len[i-1];
	}
	//因为200位呢,需要用大数。 
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		scanf("%s",s);
		m=strlen(s);
		for(i=len[m]-1;i>=0;i--)
		    printf("%d",num[m][i]);
		printf("\n");
	}
	return 0;
}


 

1410121949-hd-1sting