atcoder ABC 356-A题详解

atcoder ABC 356-A题详解

Problem Statement

You are given positive integers N, L, and R.
For a sequence A=(1,2,…,N) of length N, an operation of reversing the L-th through R-th elements was performed once.
Print the sequence after this operation.

Constraints

All input values are integers.
1≤L≤R≤N≤100

Input

The input is given from Standard Input in the following format:

N L R

Output

Let A ′=(A1′,A2′,…,AN′ ) be the sequence after the operation. Print it in the following format:

A1′ A2′ … AN′

Sample Input 1

5 2 3

Sample Output 1

1 3 2 4 5
Initially, A=(1,2,3,4,5).
After reversing the second through third elements, the sequence becomes (1,3,2,4,5), which should be printed.

Sample Input 2

7 1 1

Sample Output 2

1 2 3 4 5 6 7
It is possible that L=R.

Sample Input 3

10 1 10

Sample Output 3

10 9 8 7 6 5 4 3 2 1
It is possible that L=1 or R=N.

思路分析1:

将1~n个数存储到数组里,先要将l到r的数颠倒过来,可以用while循环实现并输出。

code:

#include <iostream>
using namespace std;
int n,l,r;
int a[110];
int mid;
int main(int argc, const char * argv[]) {
    cin>>n>>l>>r;
    for(int i=1;i<=n;i++){
        a[i]=i;
    }
    while(l<=r){
        mid=a[l];
        a[l]=a[r];
        a[r]=mid;
        l++;
        r--;
    }
    for(int i=1;i<=n;i++) cout<<a[i]<<" ";
    return 0;
}

思路分析2:

用reverse

code:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int>v;
int j,k,l;
int main(int argc, const char * argv[]) {
    cin>>j>>k>>l;
    for(int i=1;i<=j;i++)
        v.push_back(i);
    reverse(v.begin()+k-1,v.begin()+l);
    for(int i=0;i<j;i++)
        cout<<v[i]<<" ";
    return 0;
}
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值