B1008 数组元素循环右移问题 (20分)
B1008 数组元素循环右移问题 (20分)
思路
1 2 3 4 5 6
5 6 1 2 3 4
6个数,循环右移2位。
也可以理解为
先翻转
6 5 4 3 2 1
然后再两部分,分别翻转
5 6 1 2 3 4
AC代码
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
m %= n;
if (m != 0) {
reverse(begin(a), begin(a) + n);
reverse(begin(a), begin(a) + m);
reverse(begin(a) + m, begin(a) + n);
}
for (int i = 0; i < n - 1; i++)
cout << a[i] << " ";
cout << a[n - 1];
return 0;
}
另一种AC代码
#include <stdio.h>
int main() {
int a[110];
int n, m, count = 0;
scanf("%d%d", &n, &m);
m = m % n;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = n - m; i < n; i++) {
printf("%d", a[i]);
count++;
if(count < n) printf(" ");
}
for(int i = 0; i < n - m; i++) {
printf("%d", a[i]);
count++;
if(count < n) printf(" ");
}
return 0;
}
B1008 数组元素循环右移问题 (20分)的更多相关文章
- PAT Basic 1008 数组元素循环右移问题 (20 分)
一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A ...
- PTA | 1008 数组元素循环右移问题 (20分)
一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN-1 A0 ...
- PAT 1008 数组元素循环右移问题 (20)(代码)
1008 数组元素循环右移问题 (20)(20 分) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A ...
- PAT乙级 1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- PAT乙级真题1008. 数组元素循环右移问题 (20)
原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...
- PAT-乙级-1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- [C++]PAT乙级1008.数组元素循环右移问题 (20/20)
/* 1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数, ...
- PAT 乙级 1008 数组元素循环右移问题 (20) C++版
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- 【PAT】1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...
随机推荐
- 关于修改test9ui布局的一些小笔记
今早,上IT修真园里,看到师兄大娃很负责任的将我任务里的项目的排版,3,6,7的列了出来. 谢谢师兄,那么负责任的照看师弟. 言归正传,我一开始,直接按照师兄的指示,选择性的优先修改底部.效果也达到了 ...
- div多选控制
此点击按钮,弹出DIV,div内容可以多项选择,点击确定,被选项回填至文本框.功能类似之前写过的一篇日期多选,不过是在其基础上,新增点击页面其他区域,隐藏div功能. 1.css部分代码 .multi ...
- Unable to copy a file from obj\Debug to bin\Debug
1. Exit the VS2012, and then re-open the solution. 2. Clean the solution and build.
- 【起航计划 026】2015 起航计划 Android APIDemo的魔鬼步伐 25 App->Notification->Status Bar 状态栏显示自定义的通知布局,省却声音、震动
这个例子的Icons Only 和 Icons and marquee 没有什么特别好说明的. 而Use Remote views in balloon 介绍了可以自定义在Extended Statu ...
- eclipse中安装thymeleaf插件完成thymeleaf模板中自动代码提示功能
插件地址:https://github.com/thymeleaf/thymeleaf-extras-eclipse-plugin 页面有介绍如何使用:
- vos对接时业务确认及信息安全责任承诺书
业务确认及信息安全责任承诺书 双方达成一致,******网络科技有限公司向 有限公司提供中国移动网内语音线路接入服务,保证资源长期使用,保证线路资源接通率标 准,合 ...
- leetcode:栈
1. evaluate-reverse-polish-notation Evaluate the value of an arithmetic expression in Reverse Polish ...
- Ruby SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B:
最近使用ruby-china的源连接不上 使用gem update遇到这个问题, 原来是ruby没有包含SSL证书,所以Https的链接被服务器拒绝. 解决方法很简单,首先在这里下载证书(http:/ ...
- Android(java)学习笔记67:Android Studio新建工程中的build.gradle、settings.gradle
随着信息化的快速发展,IT项目变得越来越复杂,通常都是由多个子系统共同协作完成.对于这种多系统.多项目的情况,很多构建工具都已经提供了不错的支持,像maven.ant.Gradle除了借鉴了ant或者 ...
- 2018年第九届蓝桥杯【C++省赛B组】第二题 明码
汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛.16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. 一个字节可以存储8位信息,用32个字节就可以存一个汉字 ...