【C++编程基础】AOJ (C++ Programming II)—4-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.
样例输入
11
1 2 3 4 5 6 7 8 9 10 11
1
1 4 7
样例输出
1 8 9 10 5 6 7 2 3 4 11
提示:swap_ranges(A.begin() + b, A.begin()+m,A.begin() + e);

题解(代码流程)

#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()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值