hlg 1580 tell me the length

  智力题,观察上一行,有几个数字。

  比如,S[1]=1;

     S[2]=11;

     S[3]=21;

     S[4]=1211;

  这样就可以观察出来,序列一是1个1 --->  S[2] = 11 ;

            序列二是2个1 --->  S[3] = 21 ;

            序列三是1个2,2个1  ----> S[4] = 1221 ;

  知道了这个规律就好办了,然后写个函数把30个序列都给写出来就行了,一开始我还准备自己在纸上把30个序列写出来呢,后来我发现不太靠谱,好长啊!!!

  

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <queue>
 4 #include <stdlib.h>
 5 #include <algorithm>
 6 #include <math.h>
 7 #include <iomanip>
 8 #include <stack>
 9 #include <map>
10 #include <vector>
11 #include <string>
12 using namespace std;
13 /*
14     剩下的就是正常的代码了
15 -------------------------------------------------
16 */
17 string list[31];
18 void init()
19 {
20     list[1]="01";
21     int i,j,cnt,val,len;
22     string res;
23     string t="0";
24     char   temp;
25     char   BREAK;
26     for(i=2;i<=30;++i){
27         res="0";
28         len=list[i-1].length();
29         cnt=0;
30         BREAK=list[i-1][1];
31         for(j=1;j<len;++j){
32             if(BREAK!=list[i-1][j]){
33                 t[0]='0'+cnt;
34                 res=res+t;
35                 t[0]=BREAK;
36                 res=res+t;
37                 BREAK=list[i-1][j];
38                 cnt=1;
39             }else{
40                 cnt++;
41             }
42         }
43         t[0]='0'+cnt;
44         res=res+t;
45         t[0]=BREAK;
46         res=res+t;
47         list[i]=res;
48         //cout<<list[i]<<endl;
49     }
50     return;
51 }
52 int main()
53 {
54     int n;
55     init();
56     while(cin>>n&&n){
57         cout<<list[n].length()-1<<endl;
58     }
59     return 0;
60 }
原文地址:https://www.cnblogs.com/symons1992/p/3207683.html