Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300 points
Problem Statement
We define sequences Sn as follows.
- S1 is a sequence of length 1 containing a single 1.
- Sn (nn is an integer greater than or equal to 2) is a sequence obtained by concatenating Sn−1, n, Sn−1 in this order.
For example, S2 and S3 is defined as follows.
- S2 is a concatenation ofS1, 2, and S1, in this order, so it is 1,2,1.
- S3 is a concatenation of S2, 3, and S2, in this order, so it is 1,2,1,3,1,2,1
Given NN, print the entire sequence SN.
Constraints
- N is an integer.
- 1≤N≤16
Input
Input is given from Standard Input in the following format:
N
Output
Print SN, with spaces in between.
Sample Input 1 Copy
Copy
2
Sample Output 1 Copy
Copy
1 2 1
As described in the Problem Statement, S2 is 1,2,1
Sample Input 2 Copy
Copy
1
Sample Output 2 Copy
Copy
1
Sample Input 3 Copy
Copy
4
Sample Output 3 Copy
Copy
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
- S4 is a concatenation of S3, 4, and S3, in this order.
翻译:初始s1 = 1,si = si-1 + i + si-1,让你给出s这个序列
思路:类似于斐波那契数列嘛,不过那个是用数组存储,我们这个直接用string存储就好了
注意两个i与其他两个之间要加上空格,利用to_string函数将数字转化成字符
见代码
#include <bits/stdc++.h>
using namespace std;
string s[18];
int main()
{
s[1] = "1";
for (int i = 2; i < 18; i ++ )
s[i] = s[i - 1] + " " + to_string(i) + " " + s[i - 1];
int n;
cin >> n;
cout << s[n] << endl;
return 0;
}