ACM Length of S(n)

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

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).

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.

Output

Length of S(n).

Sample Input

2
5
0

Sample Output

2
6

Author

PCB@USC--Wang

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值