【PAT乙级】1008 数组元素循环右移问题 (20 分)
https://pintia.cn/problem-sets/994805260223102976/problems/994805316250615808
方法一: 用一个数组保存移动动后的数组
#include<cstdio>
#include<iostream>
using namespace std;
int a[1000];
int b[1000];
int main(void)
{int n,m;while(cin>>n>>m){for(int i=0;i<n;i++) cin>>a[i];for(int i=0;i<n;i++) b[(i+m)%n]=a[i];for(int i=0;i<n;i++) {cout<<b[i];if(i!=n-1) cout<<" ";}cout<<endl;}return 0;
}
方法二: 用队列模拟
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
int main(void)
{int n,m; cin>>n>>m;deque<int> q;for(int i=0;i<n;i++) {int number; cin>>number;q.push_back(number);}while(m--){int number=q.back();q.pop_back();q.push_front(number);}while(q.size()>1) cout<<q.front()<<" ",q.pop_front();cout<<q.front();return 0;
}
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
deque<int> q;
int n,m;
int main(void)
{cin>>n>>m;for(int i=0;i<n;i++){int x; cin>>x;q.push_back(x);}while(m--){int temp=q.back(); q.pop_back();q.push_front(temp);}for(auto t=q.begin();t!=q.end();t++){cout<<*t;if(t+1!=q.end()) cout<<" ";}return 0;
}
第三种方法:
#include<iostream>
using namespace std;
int main(){int N,M;int num[100];scanf("%d %d",&N,&M);M=M%N;//防止越界for(int i=0;i<N;i++){scanf("%d",&num[i]);}for(int i=N-M;i<N;i++){printf("%d ",num[i]);}for(int i=0;i<N-M;i++){if(i!=0) printf(" ");printf("%d",num[i]);}return 0;
}
【PAT乙级】1008 数组元素循环右移问题 (20 分)相关推荐
- C++学习之路 | PTA乙级—— 1008 数组元素循环右移问题 (20分)(精简)
1008 数组元素循环右移问题 (20分) 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A 0 A 1 ...
- PAT 乙级 1008. 数组元素循环右移问题 (20) Java版
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1--AN-1)变换为(AN-M -- AN-1 A0 ...
- pat 乙级 1008 数组元素循环右移问题(C++)
题目 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0 A1 ⋯AN−1)变换为(AN−M ⋯AN− ...
- PAT乙级(1008 数组元素循环右移问题)
一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1 ...
- PAT乙级 1008 数组元素循环右移问题
一.题目描述 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M ...
- 极高效代码(C语言):1008 数组元素循环右移问题 (20分)
立志用更少的代码做更高效的表达 Pat乙级最优化代码+题解+分析汇总-->传送门 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将 ...
- 1008 数组元素循环右移问题 (20分)
输入样例: 6 2 1 2 3 4 5 6 输出样例: 5 6 1 2 3 4 # -*- coding: utf-8 -*- import mathdef right_shift(lst, m):n ...
- 浙江大学PAT上机题解析之1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个 ...
- 7-1 数组元素循环右移问题 (20 分)
7-1 数组元素循环右移问题 (20 分) 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1 ⋯AN−1 ...
最新文章
- windows增量到linux,Beyond Compare 3在windows和linux下实现提取增量包
- 在服务器托管中对流量和带宽进行限制
- asp.net的ViewState学习
- 火狐浏览器账号登录步骤详解
- mysql jndi 实例_自己收藏-JNDI应用实例
- 奇瑞a3中控按键图解_5万元价位人生第一辆爱车如何选 奇瑞艾瑞泽5对比吉利远景...
- 前端开发工程师应该关注什么?
- ad18/ad19/ad20/ad21/ad22新版ADgaber导出(含官方教程)(含坐标文件)
- html背景位置渐变,CSS3(背景和渐变)
- 今日头条推荐算法研究
- 主板南北桥芯片都起什么作用
- Angular JS introduce
- 怎么在不影响视频清晰的条件下把qlv转成mp4格式?
- ASEMI代理AD9833BRMZ-REEL原装ADI车规级AD9833BRMZ-REEL
- 一个故事讲完进程、线程和协程
- 把图片转成像素点保存到Excel中
- 论文阅读报告:Feature Selection for Multi-label Classification Using Neighborhood Preservation,Zhiling Cai
- unicloud.dcloud
- css如何实现字体为10px
- GitHub使用教程详解(下)——Git的安装以及Git命令详解
热门文章
- 2018-11-25-今日总结
- (转)select、poll、epoll之间的区别
- bundle install 出现 #39;gem install mysql2 -v #39;0.3.15#39; succeeds before bunding #39;
- This 在 C# 中的含义
- Java容器类和包装类
- 虚拟机下的CentOS环境中安装Node.js
- BERT:代码解读、实体关系抽取实战
- Http接口设计(1)Token之前
- (chap8 确认访问用户身份的认证) SSL客户端认证
- 解决:fatal: Not a valid object name: ‘master‘问题