Length of S(n)
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
S(1)=1,
S(2)=11,
S(3)=21,
S(4)=1211,
S(5)=111221,
S(6)=312211,
……
Now, we need you to calculate the length of S(n).
Input
(1<=n<=30)
n=0 signal the end of input.
Output
Sample Input
2 5 0
Sample Output
2 6
Author
Source
2011百校联动“菜鸟杯”程序设计公开赛
我的程序:
#include<stdio.h>
#include<string.h>
char str[31][5010];
int main()
{
int n,i,j;
int k,sum;
memset(str,'\0',sizeof(str));
str[1][0]='0'+1;
for(i=2;i<=30;i++)
{
sum=1;
for(j=0,k=0;j<strlen(str[i-1]);j++)
{
if(str[i-1][j]==str[i-1][j+1])
{
sum++;
}
else
{
str[i][k++]='0'+sum;
str[i][k++]=str[i-1][j];
sum=1;
}
}
}
while(scanf("%d",&n)&&(n!=0))
{
//printf("%s\n",str[n]);
printf("%d\n",strlen(str[n]));
}
return 0;
}