蓝桥杯 彩灯与任务

题目描述

输入样例

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

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

相关推荐
h39741 小时前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
程序猿编码3 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
西飘客3 小时前
vs 怎么根据dll 生成lib文件
c++·qt
程序员老陆4 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
信奥卷王5 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵5 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG6 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z7 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛7 小时前
cpp学习笔记--常量指针
java·开发语言·算法