题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4148
就是单纯的找规律,没什么陷井
规律很简单,看一下从
S(5)=111221
变成
S(6)=312211
三个1两个2一个1,明白了吧,然后代码就简单了!
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define maxn 10000
int ans[100],first[maxn],second[maxn];
int init(){
ans[1]=1;
int *p,*q,*r,temp,k,i,j;
p=first,q=second;
p[0]=1;
for(i=2;i<31;i++){
temp=1,k=0;
for(j=1;j<ans[i-1];j++){
if(p[j]!=p[j-1]) q[k++]=temp,q[k++]=p[j-1],temp=1;
else temp++;
}
q[k++]=temp,q[k++]=p[j-1];
ans[i]=k;
r=p,p=q,q=r;
}
return 0;
}
int main(){
init();
int k;
while(scanf("%d",&k),k)printf("%d\n",ans[k]);
return 0;
}