Coder

舒心、快乐,比什么都重要

1008 数组元素循环右移问题 (20 分)

#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main(){
    int n, m;
    cin >> n >> m;
    int *p = new int[n];
    for (int i = 0; i < n; i++)
        cin >> p[i];
    m = m % n;
    reverse(p, p + n - m);  // 算法精髓
    reverse(p + n - m, p + n);
    reverse(p, p + n);
    for (int i = 0; i < n; i++){
        if (i == 0)
            cout << p[i];
        else
            cout << ' ' << p[i];
    }
    delete[] p;
    cout << endl;
    return 0;
}

 

posted @ 2019-04-16 01:41  H123K  阅读(83)  评论(0编辑  收藏  举报