*大数求和加上斐波拉契
Problem O 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 Sample Output 1 大数求和加上斐波拉契 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int i,j,cnt,r,n,len1,len2,k,m,temp,carry,t,x,flag;
char c,a[1000],b[1000],d[1000];
scanf("%d",&n);
getchar();
for(i=0; i<n; i++)
{
cnt=0;
while(1)
{
scanf("%c",&c);
if(c=='1')
cnt++;
else
break;
}
sprintf(a,"%d",1);
sprintf(b,2);
for(j=3; j<=cnt; j++)
{
len1=strlen(a);
len2=strlen(b);
x=len1>len2?len1:len2;
carry=0;
t=x;
memset(d,'0',t);
d[t+1]=' ';
for(k=len1-1,m=len2-1;k>=0||m>=0;k--,m--,t--)
{
temp=carry;
if(k>=0)temp+=a[k]-'0';
if(m>=0)temp+=b[m]-'0';
if(temp>=10)
{
d[t]=temp-10+'0';
carry=1;
}
else
{
d[t]=temp+'0';
carry=0;
}
}
d[t]=carry+'0';
strcpy(a,b);
strcpy(b,d);
}
flag=0;
if(cnt==1)
printf("1");
else if(cnt==2)
printf("2");
else
for(j=0;j<=x;j++)
{
if(d[j]!='0')
flag=1;
if(flag==1)
printf("%c",d[j]);
}
printf("n");
}
return 0;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |