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;
}
时间: 2024-08-28 01:07:50

HDU 4148 Length of S(n)(规律题)的相关文章

HDU 4148 Length of S(n)(字符串)

题目 字符串处理 题意要猜,解析见代码: /* 这题每个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 <cstdio> #include<iostream> #include <cstring> #include <algorithm> using namespac

2014 HDU多校弟八场H题 【找规律把】

看了解题报告,发现看不懂 QAQ 比较简单的解释是这样的: 可以先暴力下达标,然后会发现当前数 和 上一个数 的差值是一个 固定值, 而且等于当前数与i(第i个数)的商, 于是没有规律的部分暴力解决,有规律的套公式 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring&g

HDU 4279 Number 规律题

题意: 定义函数F(x) : 区间[1,x]上的y是满足:GCD(x,y)>1 && x%y>0的 y的个数. 问:对于任意区间[l,r] 上的F(l···r) 有几个函数值是奇数的. 打表找规律. 打的是[1,x]区间的结果 把所有结果不相同的值打出来(因为结果是递增的,所以只观察不相同的结果) 发现:ans = x/2-2 || x/2-1 再把所有结果不同 && x/2-1的值打出来 发现 sqrt(x) &1 == 1 得到:ans = x/2-

HDU 4937 Lucky Number 规律题_(:зゝ∠)_

把所有合法的进制打出来会发现合法的进制都是在 n/3 n/4 n/5的边上 然后暴力边上的进制数.. #include <cstdio> #include <set> typedef long long ll; bool ok(ll x, ll y) { ll v; while (x > 0) { v = x % y; if (v != 3 && v != 4 && v != 5 && v != 6) return false;

HDU 4940 Destroy Transportation system 规律题

答案只有2种情况,所以ans = rand()%2; if(ans)puts("happy") else puts("unhappy"); == 想过无源汇的网络流,还是比较麻烦的,然后没往下想... 设s点集有一些点, 多加一个点一定是y增加比较快_(:зゝ∠)_ 然后设s点集只有一个点 #include <cstdio> #include <map> #include <set> #include <algorithm&

HDU 4952 Number Transformation 规律题

打表可以知道到后面增量都一样了,, 推论就是  i 和 i+1 互质 #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const ll mx = 120000; int main() { int cas = 0; ll x, k, y, dis, i; while (

HDU 4572 Bottles Arrangement(找规律,仔细读题)

题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1,m-2,m-2,……,2,2,1,1求出前m个数字的和就是答案. //发现案例符合(之前的代码第二天发现案例都跑不对,真不知道我当时眼睛怎么了) #include <iostream> #include<stdio.h> #include<string.h> #inclu

HDU 4923 Room and Moor(瞎搞题)

瞎搞题啊.找出1 1 0 0这种序列,然后存起来,这种情况下最好的选择是1的个数除以这段的总和.然后从前向后扫一遍,变扫边进行合并.每次合并,合并的是他的前驱.这样到最后从t-1找出的那条链就是最后满足条件的数的大小. Room and Moor Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 307    Accepted Su

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there