2013 - ECJTU 暑期训练赛第六场-problem-I

I -I
Crawling in process... Crawling failed Time Limit:1000MS    Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
这题我刚做的时候感觉很蛋疼,因为实在看不懂题目在搞什么,后来经过学长点播,立马明白了
AC代码+解释:
/*
这题每个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和1个1=1211
....
.....
.......
*/
#include<iostream>//因为题目给的测试数据不大,所以直接打表
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
const int MAX=5000;
char s[31][MAX];//我测试下,到了S(30)左右会长度差不多快到5000
using namespace std;//这里S[n][n]是模拟题目S(n)
void counnt()
{
    s[1][0]='1';
    s[2][0]='1';
    s[2][1]='1';
    s[3][0]='2';
    s[3][1]='1';
    int i,j,k,len;
    int num;
    for(i=4;i<=30;i++)
    {
        len=strlen(s[i-1]);
        num=1;
        k=0;
        for(j=1;j<len;j++)
        {
            if(s[i-1][j]==s[i-1][j-1])//如果前一个数字等于这一个数字那么+1
            {
                num+=1;
            }
            else//否则
            {
                if(num>9)
                {
                    s[i][k]=num%10+48;
                    k+=1;
                    num/=10;
                    s[i][k]=num+48;
                    k+=1;
                }
                else
                {
                    s[i][k]=num+48;
                    k+=1;
                    s[i][k]=s[i-1][j-1];
                    k+=1;
                    num=1;
                }
            }
        }
        if(num>9)
        {
            s[i][k]=num%10+48;
            k+=1;
            num/=10;
            s[i][k]=num+48;
            k+=1;
        }
        else
        {
            s[i][k]=num+48;
            k+=1;
            s[i][k]=s[i-1][j-1];
            k+=1;
        }
    }
}
int main()
{
    int n,m;
    counnt();
    while(cin>>n,n)
    {
        m=strlen(s[n]);
        cout<<m<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值