队列运用 数组元素循环右移问题

 

题解:

网上看了很多答案,感觉都是挺复杂的而且麻烦。这里我写下我自己的思路(大佬轻喷)

双端队列存储数组,如果要右移的话,只要将最后一位移动到第一位即可,移动几位,循环几次即可。

代码:

#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;
}

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
循环队列是一种特殊的队列,它可以充分利用数组空间,实现队列的基本操作。在循环队列中,队列的头尾相连,当队列满时,可以将队列头指针指向数组的第一个位置,实现循环利用。C++中实现循环队列可以使用数组和指针结构体来实现。 以下是使用数组实现循环队列C++代码: ```c++ #define MAXSIZE 10 // 定义循环队列的最大长度 typedef int ElementType; // 定义队列元素类型 class CircularQueue { private: ElementType data[MAXSIZE]; // 队列数组 int front; // 队头指针 int rear; // 队尾指针 public: CircularQueue() { // 构造函数,初始化队头和队尾指针 front = rear = 0; } bool isEmpty() { // 判断队列是否为空 return front == rear; } bool isFull() { // 判断队列是否为满 return (rear + 1) % MAXSIZE == front; } void enQueue(ElementType x) { // 入队 if (isFull()) { cout << "Queue is full!" << endl; return; } data[rear] = x; rear = (rear + 1) % MAXSIZE; } void deQueue() { // 出队 if (isEmpty()) { cout << "Queue is empty!" << endl; return; } front = (front + 1) % MAXSIZE; } void printQueue() { // 打印队列 if (isEmpty()) { cout << "Queue is empty!" << endl; return; } int i = front; while (i != rear) { cout << data[i] << " "; i = (i + 1) % MAXSIZE; } cout << endl; } }; ``` 使用指针结构体实现循环队列C++代码如下: ```c++ #define MAXSIZE 10 // 定义循环队列的最大长度 typedef int ElementType; // 定义队列元素类型 struct Queue { ElementType* data; // 队列数组 int front; // 队头指针 int rear; // 队尾指针 int size; // 队列长度 int capacity; // 队列容量 }; Queue* createQueue(int k) { // 创建空队列 Queue* q = new Queue; q->data = new ElementType[k]; q->front = q->rear = 0; q->size = 0; q->capacity = k; return q; } bool isEmpty(Queue* q) { // 判断队列是否为空 return q->size == 0; } bool isFull(Queue* q) { // 判断队列是否为满 return q->size == q->capacity; } void makeEmpty(Queue* q) { // 置空 q->front = q->rear = 0; q->size = 0; } int lengthOfQueue(Queue* q) { // 计算队列长度 return q->size; } void enQueue(Queue* q, ElementType x) { // 入队 if (isFull(q)) { cout << "Queue is full!" << endl; return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % q->capacity; q->size++; } void deQueue(Queue* q) { // 出队 if (isEmpty(q)) { cout << "Queue is empty!" << endl; return; } q->front = (q->front + 1) % q->capacity; q->size--; } void printQueue(Queue* q) { // 打印队列 if (isEmpty(q)) { cout << "Queue is empty!" << endl; return; } int i = q->front; while (i != q->rear) { cout << q->data[i] << " "; i = (i + 1) % q->capacity; } cout << endl; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xun_M

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值