蓝桥杯 彩灯与任务

题目描述

输入样例

5 5

5 4 3 3 9

R 1

C 4

R 5

A 3

R 2

输出样例

5

3

3

思路

第一眼读不懂旋转是啥意思,根据样例连蒙带猜猜出来,其实就是把整个数组中的挪动几个位置。也很自然的按照题意写出来如下代码:

cpp 复制代码
#include <iostream>
using namespace std;
int N;
int A[1000];
int B[1000];
void rotate(int x){
  for(int i=0;i<N;i++){
    B[(i+x)%N]=A[i];
  }
   for(int i=0;i<N;i++){
    A[i]=B[i];
  }
}
int main()
{
  int M;
  cin>>N>>M;
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  char type;
  int num;
  int now = 0;
  while(M--){
    cin>>type>>num;
    if(type=='C'){ 
      rotate(N-num);
     }
    if(type=='A'){
        rotate(num);
    }
    if(type=='R'){
      cout<<A[(num-1)%N]<<endl;
    }
  }
  return 0;
}

正好有个拿过国一的大佬路过,他帮我改了,写出来如下代码 :

cpp 复制代码
#include <iostream>
using namespace std;
int N;
int A[1000];
int B[1000];
// void rotate(int x){
//   for(int i=0;i<N;i++){
//     B[(i+x)%N]=A[i];
//   }
//    for(int i=0;i<N;i++){
//     A[i]=B[i];
//   }
// }
int main()
{
  int M;
  cin>>N>>M;
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  char type;
  int num;
  int now = 0;
  while(M--){
    cin>>type>>num;
    if(type=='C'){
      now -= num;
    }
    if(type=='A'){
      now += num;
    }
    if(type=='R'){
      now %= N;
      cout<<A[(num - 1 - now + N)%N]<<endl;
    }
  }
  return 0;
}

可以看到,只需要记录下偏移量,不需要真正的偏移。强!

相关推荐
eggwyw6 小时前
C++ union 的一种妙用法
开发语言·c++
罗西的思考6 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
ShineWinsu8 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
小七在进步9 小时前
string的实现(1)
c++·笔记·visual studio
kyle~11 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
敲上瘾11 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
牢姐与蒯12 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好13 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
lcj251113 小时前
[C++] C++11
开发语言·c++·笔记
电科_银尘14 小时前
【大模型算法与工程 02】线性回归与逻辑回归:从损失函数到决策边界
算法·逻辑回归·线性回归