Length of S(n)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 156 Accepted Submission(s): 99
Problem 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
这个题主要是找规律,规律找到了这道题就迎刃而解了,悲剧的是比赛时找不到规律啊,,自己弱爆了,,规律是后一个字符串是对前一个字符串的描述,,,比如s[3]描述的是s[2]中两个1,,,
AC代码:
#include<iostream> #include<string> using namespace std; string s,s1; int main() { int n; while(cin>>n&&n) { if(n==1){cout<<"1"<<endl;continue;} s="1"; for(int i=2;i<=n;++i) { s1=""; int m=s.size(); for(int j=0;j<m;++j) { char ch=s[j]; int ss=j; for(int k=j+1;k<m;++k) {if(s[k]!=ch) break; ss=k; } int num=ss-j+1; j=ss; if(num<10) s1+=num+'0'; else{ int a=num/10; int b=num%10; s1+=a+'0'; s1+=b+'0'; } s1+=ch; } s=s1; } cout<<s.size()<<endl; }return 0; }

被折叠的 条评论
为什么被折叠?



