4-A-reverse
题目
Reverse Write a program which reads a sequence of integers A={a0,a1,…,an−1} and reverse specified elements by a list of the following operation: reverse(b,e): reverse the order of ab,ab+1,…,ae−1
输入
The input is given in the following format. n a0 a1…,an−1 q b1 e1 b2 e2 : bq bq In the first line, n (the number of elements in A) is given. In the second line, ai (each element in A) are given. In the third line, the number of queries q is given and each query is given by two integers biei in the following q lines.
输出
Print all elements of A in a line after performing the given operations. Put a single space character between adjacency elements and a newline at the end of the last element.
样例输入
8
1 2 3 4 5 6 7 8
2
1 6
3 8
样例输出
1 6 5 8 7 2 3 4
题解(代码流程)
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++)cin >> A[i];
cin >> q;
while (q--) {
int b, e;
cin >> b >> e;
reverse(A.begin() + b, A.begin() + e);//对数组范围内b~e的数进行反转
}
for (auto e:A) {
cout << e << " ";
}
cout << endl;
return 0;
}
小结
学会简单运用reverse来反转范围中的元素顺序
reverse(BidirIt first, BidirIt last )
first, last - 要反转的元素的范围
BidirIt 必须满足值可交换 (ValueSwappable) 和 遗留双向迭代器 (LegacyBidirectionalIterator) 的要求。
最后
以上就是过时短靴最近收集整理的关于AOJ (C++ Programming II)—4-A-reverse4-A-reverse的全部内容,更多相关AOJ内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复