我是靠谱客的博主 谦让毛衣,这篇文章主要介绍AOJ (C++ Programming II)—4-C-swap4-C-swap,现在分享给大家,希望可以做个参考。

4-C-swap

题目

Swap Write a program which reads a sequence of integers A={a0,a1,…,an−1} and swap specified elements by a list of the following operation: swapRange(b,e,t): For each integer k (0≤k<(e−b), swap element (b+k) and element (t+k).
输入
The input is given in the following format.
n
a0 a1…,an−1
q
b1 e1 t1
b2 e2 t2
:
bq eq tq
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 three integers bi ei ti 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.
样例输入
复制代码
1
2
3
4
5
11 1 2 3 4 5 6 7 8 9 10 11 1 1 4 7
样例输出
复制代码
1
2
1 8 9 10 5 6 7 2 3 4 11
提示:swap_ranges(A.begin() + b, A.begin()+m,A.begin() + e);

题解(代码流程)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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, t; cin >> b >> t >> e; swap_ranges(A.begin() + b, A.begin() + t, A.begin() + e);//调用swap_ranges函数,代入三个迭代器参数 } for (auto e:A) { cout << e << " "; } cout << endl; return 0; }

小结

学会运用swap_ranges函数来交换范围中的元素

swap_ranges()

最后

以上就是谦让毛衣最近收集整理的关于AOJ (C++ Programming II)—4-C-swap4-C-swap的全部内容,更多相关AOJ内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部