1008 数组元素循环右移问题 (20 分)

一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A​0​​A​1​​⋯A​N−1​​)变换为(A​N−M​​⋯A​N−1​​A​0​​A​1​​⋯A​N−M−1​​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:

每个输入包含一个测试用例,第1行输入N(1)和M(≥);第2行输入N个整数,之间用空格分隔。

输出格式:

在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

输入样例:

6 2
1 2 3 4 5 6

输出样例:

5 6 1 2 3 4

C++实现:

分析:

实现数组右移分3步:

(1)将数组颠倒(2)将数组 [ 0, m ) 颠倒(3)将数组 [ m, length) 颠倒

注意:

(1)右移位数 M 可能会比元素个数 N 大,要先进行 M % N 的操作

(2)判断 M 是否为 0

 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
//先将数组倒置,然后再将数组后的M位再倒置
int N, M;
cin >> N >> M; //元素个数,后移位数
vector<int> v(N);
for (int i = ; i < N; ++i)
{
cin >> v[i];
} M %= N; //防止M比N大
if (M != )
{
reverse(v.begin(), v.begin() + N); //第一次将所有数组倒置
//由1-2-3-4-5-6变成6-5-4-3-2-1 reverse(v.begin(), v.begin() + M); //第二次将部分数组倒置
//由6-5-4-3-2-1变成5-6-4-3-2-1 reverse(v.begin() + M, v.end()); //第三次将部分数组倒置
//由6-5-4-3-2-1变成5-6-1-2-3-4
}
vector<int>::iterator it; for (it = v.begin(); it != v.end() - ; ++it)
{
cout << *it << " "; }
cout << *it;
return ;
}

Java实现:

 import java.util.Scanner;

 public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
int a[] = new int[n];
int i = 0;
for (i = 0; i < n; i++) {
a[i] = input.nextInt();
}
if (n < m) {
m = m % n;
}
if (n == m) {
for (i = 0; i < n; i++) {
if (i != 0) {
System.out.print(" ");
}
System.out.print(a[i]);
}
} else {
for (int j = n - m; j < n; j++) {
System.out.print(a[j] + " ");
}
for (int j = 0; j < n - m; j++) {
if (j != 0) {
System.out.print(" ");
}
System.out.print(a[j]);
}
}
}
}

PAT 乙级 1008.数组元素循环右移问题 C++/Java的更多相关文章

  1. PAT乙级 1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

  2. [C++]PAT乙级1008.数组元素循环右移问题 (20/20)

    /* 1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数, ...

  3. PAT 乙级 1008 数组元素循环右移问题 (20) C++版

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

  4. PAT 乙级 -- 1008 -- 数组元素循环右移问题

    题目简述 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN ...

  5. 【PAT】1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...

  6. PAT Basic 1008 数组元素循环右移问题 (20 分)

    一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A​0​​A​1​​⋯A​N−1​​)变换为(A​N−M​​⋯A​N−1​​A ...

  7. PAT乙级真题1008. 数组元素循环右移问题 (20)

    原题: 1008. 数组元素循环右移问题 (20) 时间限制400 ms内存限制65536 kB 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M&g ...

  8. PAT 1008 数组元素循环右移问题 (20)(代码)

    1008 数组元素循环右移问题 (20)(20 分) 一个数组A中存有N(N&gt0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A ...

  9. PAT-乙级-1008. 数组元素循环右移问题 (20)

    1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...

随机推荐

  1. Spring Boot 构造器参数绑定,越来越强大了!

    在之前的文章:Spring Boot读取配置的几种方式,我介绍到 Spring Boot 中基于 Java Bean 的参数绑定,在一个 Java Bean 类上用 @ConfigurationPro ...

  2. Kubeadm部署安装kubernetes1.12.1

    1.环境准备(这里是master) CentOS 7.6 两台配置如下,自己更改主机名,加入hosts, master和node 名字不能一样 # hostname master # hostname ...

  3. Linux中文件权限查看和修改

    权限定义 linux文件权限分为:r读权限(4).w写权限(2).x执行权限(1) linux权限对象分为:拥有者.组用户.其他用户 权限修改: chown user:group /usr/local ...

  4. xshell 快速复制粘贴的方法

    xshell快速复制粘贴的方法<img src="http://newmiracle.cn/wp-content/uploads/2017/01/QQ截图20170113163139- ...

  5. linux ffmpeg 源码安装教程

    AMR格式是智能手机上的常用音频文件格式,比MP3格式的压缩比大.同样时长的AMR文件大概是MP3的十分之一,所以在移动互联项目中应用比较广泛.但目前AMR格式在个人电脑上应用较少,所以目前大部门播放 ...

  6. Python 发送邮件 and 编辑Excel

    记录一下Python 发送邮件的代码,这是半年前写的,不知道现在有什么类库的改动. 类库 import smtplib from email.mime.text import MIMEText fro ...

  7. CentOS7-部署测试Apollo

    linux部署apollo环境要求:jdk1.8.mysql5.7 centos7安装jdk1.8跟mysql5.7可以参考我这两篇文章 https://www.cnblogs.com/reasonz ...

  8. Baidu UEditor .net 下修改默认上传路径

    public override void Process() { byte[] uploadFileBytes = null; string uploadFileName = null; if (Up ...

  9. C语言输入带空格的字符串

    参考:https://blog.csdn.net/vincemar/article/details/78750435 因为: scanf("%s",str); 遇到空格就停止接收后 ...

  10. FPGA 软件平台

    FPGA软件平台 系统 --> windows 7 xilinx --> vivado 2016.4 xilinx --> ISE 14.7 Altera --> quartu ...