标签:
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
参考代码:
1 //本地打表 2 #include <iostream> 3 #include <cstdio> 4 #include <cmath> 5 #include <ctype.h> 6 #include <cstring> 7 #include <algorithm> 8 #include <string> 9 using namespace std; 10 #define M 10000 + 500 11 char a[30][M]; 12 int b[30]; 13 int main() 14 { 15 freopen("in.txt","r",stdin); 16 int t,i,j; 17 a[0][0] = ‘1‘; 18 a[0][1] = ‘\0‘; 19 b[0] = 1; 20 for(i = 0; i < 31; i ++) 21 { 22 int cnt = 0,t = 0; 23 char temp = a[i][0]; 24 for(j = 0; a[i][j] != ‘\0‘; j ++) 25 { 26 if(temp == a[i][j]) 27 28 { 29 while(temp == a[i][j]) 30 { 31 j ++; 32 cnt ++; 33 } 34 } 35 a[i + 1][t ++] = cnt + ‘0‘; 36 a[i + 1][t ++] = temp; 37 temp = a[i][j]; 38 cnt = 0; 39 j --; 40 } 41 a[i + 1][t] = ‘\0‘; 42 } 43 for(int i = 0; i < 31; i ++) 44 printf("%d,",strlen(a[i])); 45 int iu; 46 while(scanf("%d",&iu) && iu) 47 printf("%d\n",b[iu]); 48 return 0; 49 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <ctype.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <string> 8 using namespace std; 9 #define M 10000 + 500 10 int b[33] = {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,5808}; 11 int main() 12 { 13 //freopen("in.txt","r",stdin); 14 int i; 15 while(scanf("%d",&i) && i) 16 printf("%d\n",b[i - 1]); 17 return 0; 18 }
标签:
原文地址:http://www.cnblogs.com/zouqihan/p/4482345.html