Queue uses array elements to move right

 

answer:

I read a lot of answers on the Internet, and I feel that they are quite complicated and troublesome. Here I write down my own ideas (big brother light spray)

Use a double-ended queue to store the array. If you want to move to the right, just move the last bit to the first , move a few bits, and cycle a few times .

code:

#include <bits/stdc++.h>
using namespace std;
int main() {
    deque<int> dp;
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        dp.push_back(a);
    }
    for (int i = 0; i < m; i++) { 
        dp.push_front(dp[dp.size() - 1]); //将最后一位复制到第一位
        dp.pop_back(); //删除最后一位
    }
    for (int i = 0; i < n; i++) {
        printf("%d", dp[i]);
        if (i < n - 1) printf(" ");
    }
    return 0;
}

 

 

Guess you like

Origin blog.csdn.net/qq_45462923/article/details/113986305