C - 1 2 1 3 1 2 1

原题链接

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值