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