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).
InputThe input consists of multiple test cases. Each test case contains one integers n.
(1<=n<=30)
N=0 signal the end of input.
OutputLength of S (n).
Sample Input
250
Sample Output
26 Topic Analysis:/** * @xiaoran * Law questions, see if you can find this rule, I still quite lucky to find the law, * If you can not find the problem. The law is as follows: *s[i] is to see s[i-1] formed, for example; s[1]=1; *s[2]=11, representing S[i-1=1] there are 1 1 *s[3]=21, representing s[i-1=2] with 2 1 *s[4]=1211, and s[i-1=3] There is a 1, a 2, 2 1, * See, notice from left to right, can't jump, then simulate the law to generate a string bar */
AC Code:
/** * @xiaoran * Law questions, see if you can find this rule, I still quite lucky to find the law, * if you can not find the problem. The law is as follows: *s[i] is to see s[i-1] formed, for example; s[1]=1; *s[2]=11, representing S[i-1=1] there are 1 1 *s[3]=21, representing s[i-1=2] there are 2 1 *s[4]=1211, the representative is s[i-1=3] there is a 1, a 2, 2 1, * understand it, notice from left to right to see, can't jump, So let's simulate the pattern of strings. */#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 longusing namespace std;// Length of the first 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;}
HDU 4148 Length of S (n) (law problem)