Length of S(n)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1140 Accepted Submission(s): 661
Problem Description
A number sequence is defined as following:
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).
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
The input consists of multiple test cases. Each test case contains one integers n.
(1<=n<=30)
n=0 signal the end of input.
(1<=n<=30)
n=0 signal the end of input.
Output
Length of S(n).
Sample Input
2 5 0
Sample Output
2 6
题目意思:
这题S(n)是描述S(n-1)值
例如:
S(1) = 1;
S(2) = 11;即描述S(1)有1个1 = 11
S(3) = 21;即描述S(2)有2个1 = 21
S(4) = 1211;即描述S(3)有1个2和2个1 = 1211;
#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;
}