hdoj 4148 Length of S(n) 【打表】

本文详细解析了数列S(n)的定义及其计算长度的方法,通过实例展示了如何从基本序列出发,逐步推导出任意n值时序列的长度。包括输入输出样例的说明和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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
 
题目意思:
 
这题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;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值