D
e
s
c
r
i
p
t
i
o
n
Description
Description
S今天给你出了一道找规律题,题目如下: 有如下的数列1,11,21,1211,111221,312211,…… 小S问你这个数列的第N项是多少,而你一头雾水根本找不出规律。 聪明的小R悄悄地告诉你是这样的 1, 上一个数是一个1,写作11 上一个数是两个1,写作21 上一个数是一个2,一个1,写作1211 上一个数是一个1,一个2,两个1,写作111221, ………… 相信聪明的你一定可以解决这个问题。
I
n
p
u
t
Input
Input
第一行包括一个正整数N。
O
u
t
p
u
t
Output
Output
一行一个正整数(注意数字可能会唱过长整形,请注意用数组或者字符串存储)
S
a
m
p
l
e
Sample
Sample
I
n
p
u
t
Input
Input
6
S
a
m
p
l
e
Sample
Sample
O
u
t
p
u
t
Output
Output
312211
E
x
p
l
a
i
n
Explain
Explain
对于100%的数据1<-N<=30。
T
r
a
i
n
Train
Train
o
f
of
of
T
h
o
u
g
h
t
Thought
Thought
直接按照题意模拟就好了,没什么难度 (比赛时看到whd巨佬打表。。。)
C
o
d
e
Code
Code
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std
;
int a
[55],n
;
string s
[55];
int main()
{
scanf("%d",&n
);
s
[1]=1+48;
for (int i
=2; i
<=n
; ++i
)
{
int sum
=0; a
[s
[i
-1][0]-48]++,sum
=s
[i
-1][0]-48;
for (int j
=1; j
<=s
[i
-1].length(); ++j
)
{
if (s
[i
-1][j
]==s
[i
-1][j
-1]) a
[sum
]++;
if (s
[i
-1][j
]!=s
[i
-1][j
-1]) {
s
[i
]=s
[i
]+(char)(a
[sum
]+48)+(char)(sum
+48);
a
[sum
]=0;
if (s
[i
-1][j
]>='0' && s
[i
-1][j
]<='9')
sum
=s
[i
-1][j
]-48;
else sum
=0;
a
[sum
]++;
}
}
memset(a
,0,sizeof(a
));
}
cout
<<s
[n
]<<endl
;
return 0;
}