HDU 4148 Length of S(n)(规律题)

Problem Description

http://acm.hdu.edu.cn/showproblem.php?pid=4148

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 题目分析:
/**
  *@xiaoran
  *规律题,就看能不能找到这个规律了,我还是挺幸运的找到了规律,
  *如果找不到还是看题解吧。规律如下:
  *s[i]是看s[i-1]形成的,例如;s[1]=1;
  *s[2]=11;代表的是s[i-1=1]里有1个1
  *s[3]=21;代表的是s[i-1=2]里有2个1
  *s[4]=1211;代表的是s[i-1=3]里有1个1,1个2,2个1,
  *明白了吧,注意从左向右看,不能跳跃,那么来模拟规律生成字符串吧
  */
AC代码:
/**
  *@xiaoran
  *规律题,就看能不能找到这个规律了,我还是挺幸运的找到了规律,
  *如果找不到还是看题解吧。规律如下:
  *s[i]是看s[i-1]形成的,例如;s[1]=1;
  *s[2]=11;代表的是s[i-1=1]里有1个1
  *s[3]=21;代表的是s[i-1=2]里有2个1
  *s[4]=1211;代表的是s[i-1=3]里有1个1,1个2,2个1,
  *明白了吧,注意从左向右看,不能跳跃,那么来模拟规律生成字符串吧
  */
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<cmath>
#define LL long long
using namespace std;
//前30个的长度
const int a[33]={0,1,2,2,4,6,6,8,10,14,20
            ,26,34,46,62,78,102
            ,134,176,226,302,408
            ,528,678,904,1182,1540
            ,2012,2606,3410,4462
};
string s[33];
int main()
{
    s[1]="1";
    for(int i=2;i<=30;i++){
        int j,k=1,len=s[i-1].size();
        for(j=1;j<len;j++){
            if(s[i-1][j]==s[i-1][j-1]){
                k++;
            }else{
                s[i]+=char (k+'0');
                s[i]+=s[i-1][j-1];
                k=1;
            }
        }
        s[i]+=char (k+'0');
        s[i]+=s[i-1][j-1];
        cout<<s[i].size()<<",";
    }
    int n;
    while(cin>>n&&n){
        cout<<s[n].size()<<endl;
        //cout<<a[n]<<endl;
    }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值