一 、问题描述

     原题描述

  将长度为n的整形数组A进行右移m位操作, [A0 A1 A2 A3 ... Am...An-1]变为[An-m...An-1 A0 A1 A2 A3 ...An-m-1 ]

  输入格式:

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

  输出格式:

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

  输入样例:

  6 2   

  1 2 3 4 5 6

  输出样例:

  5 6 1 2 3 4

二、解题思路

  比较常用的思路就是逆序打印再正序打印,和三次逆序翻转。

  显然翻转操作解题更符合题意,通过三次翻转操作,可得到右移的数组。首先对数组A整体进行翻转,然后再对A[0]到A[m-1]进行翻转,最后再对A[m]到A[n-1]这部分进行翻转操作,最后得到右移效果。

  下图为n= 10, m = 4的例子:

  解题代码如下:

#include <stdio.h>

void printMove( int A[], int arraySize )
{
int j;
for ( j = ; j < arraySize; j++ ) {
printf("%d", A[j]);
if ( j < (arraySize - ) ) {
printf(" ");
}
}
} void move( int A[], int left, int right)
{
while ( left < right ) {
int temp = A[left];
A[left] = A[right];
A[right] = temp; left++;
right--;
}
} void move_A( int A[], int left, int right )
{ // 从左到右进行元素交换
int i, mid, tmp; mid = ( right - left + ) / ;
for ( i = ; i < mid; i++ ) {
tmp = A[i + left];
A[i + left] = A[right - i];
A[right - i] = tmp;
}
} int main(int argc, char *argv[])
{
int i, j;
int arraySize, moveStep;
scanf("%d %d", &arraySize, &moveStep); int A[arraySize];
for ( i = ; i < arraySize; i++ ) {
scanf("%d", &A[i]);
//A[i] = i+1;
} /* three step */
if ( moveStep > ) {
moveStep = moveStep % arraySize;
move( A, , arraySize - );
//printMove( A, arraySize ); printf("\n"); move( A, , moveStep - );
//printMove( A, arraySize ); printf("\n"); move( A, moveStep, arraySize - );
printMove( A, arraySize ); //printf("\n");
} else {
printMove( A, arraySize ); //printf("\n");
} return ;
}

  

  Python版本:

nums = input()
L = nums.split(" ")
A, B = int(L[0]), int(L[-1])
B = B % A numChars = input().split(" ")
Array = [ int(i) for i in numChars ] if B > 0:
Array = Array[::-1]
leftA = Array[:B][::-1]
rightA = Array[B:][::-1]
Array = leftA + rightA length= len(Array)
for i in range(length):
print( Array[i], end="")
if i < (length - 1):
print( " ", end="")
i = i + 1

PTA(BasicLevel)-1008数组元素循环右移问题的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. PAT 乙级 1008.数组元素循环右移问题 C++/Java

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

  9. PTA(Basic Level)1008.数组元素循环右移问题

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

随机推荐

  1. iOS8模糊效果UIVisualEffectView的使用

    iOS8模糊效果UIVisualEffectView的使用 效果: 源码: // // ViewController.m // EffectView // // Created by XianMing ...

  2. 计算机作业(HTML简单框架网页制作) 物联网 王罗红

  3. 用timer自定义计划任务时间

    应业务需求,需要将指定程序,按照指定时间进行运行, 而windows计划任务最小运行间隔时间为1分钟,完全不能满足当前需求, 有两种方案,一种是安装win服务方式,考滤到维护困难,另一种是timer方 ...

  4. 深入浅出SharePoint——使用WinDbg进行调试

  5. linux性能系列--cpu

    一.先看看什么是CPU? 回答:中央处理器(Central Processing Unit)的缩写,即CPU,CPU是电脑中的核心配件,只有火柴盒那么大,几十张纸那么厚,但它却是一台计算机的运算核心和 ...

  6. 消息中间件--"rocketmq"02之QuickStart

    依赖 <dependency> <groupId>com.alibaba.rocketmq</groupId> <artifactId>rocketmq ...

  7. gluoncv 导入方式

    了解了sys.path和python 的import 的话,之前修改gluoncv 的方式就可以有了新的简单的方法: 如果你pip install gluoncv的,然后脚本又git clone了,发 ...

  8. nd.array.where

    http://mxnet.apache.org/api/python/ndarray/ndarray.html#mxnet.ndarray.where Return the elements, eit ...

  9. VIM在Win7上的安装教程

    1.下载 目前VIM在其官网上的最新版本为7.4,Windows版本名称为GVIM,在百度软件中可以下载GVIM的最新版本,建议 在百度上下载,因为比较快.在百度上搜索"GVIM" ...

  10. ethereumjs/ethereumjs-blockchain-1-简介和API

    https://github.com/ethereumjs/ethereumjs-blockchain SYNOPSIS概要 A module to store and interact with b ...