PTA(BasicLevel)-1008数组元素循环右移问题
一 、问题描述
将长度为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数组元素循环右移问题的更多相关文章
- 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)(20 分) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A ...
- 【PAT】1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN- ...
- PAT 乙级 1008.数组元素循环右移问题 C++/Java
1008 数组元素循环右移问题 (20 分) 题目来源 一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A0A1⋯ ...
- PTA(Basic Level)1008.数组元素循环右移问题
一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A0A1⋯AN−M−1)(最 ...
随机推荐
- 如何理解springaop
初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是oop的一种有益补充等等,一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后,我才发现:它就是一些 ...
- [COGS 2066]七十和十七
2066. 七十和十七 ★★★ 输入文件:xvii.in 输出文件:xvii.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 七十君最近爱上了排序算法,于是Ta ...
- [COGS 0407][NOIP 2009] 靶形数独
407. [NOIP2009] 靶形数独 ★★ 输入文件:sudoku.in 输出文件:sudoku.out 简单对比时间限制:5 s 内存限制:128 MB [问题描述] 小城和小华 ...
- 78、WebClient实现上传下载 System.Net、System.Uri类
高层类.使用简单.均支持异步版本.支持http,https,fpt,files等URI. 一.下载 方法: Stream= client.OpenRead(serverUri): 打开一个可读的流.对 ...
- psql: FATAL: role “postgres” does not exist
I'm a postgres novice. I installed the postgres.app for mac. I was playing around with the psql comm ...
- JavaScript事件的委派与事件的绑定
事件的委派 在很多需求中,通常元素是动态创建添加到一个父元素中的,这时候我们点击新增的元素是没有反应的 <script type="text/javascript"> ...
- BZOJ1012:[JSOI2008]最大数maxnumber(线段树)
Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L 个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. ...
- gluoncv rpn 正负样本
https://github.com/dmlc/gluon-cv/blob/master/gluoncv/model_zoo/rpn/rpn_target.py def forward(self, i ...
- gluoncv faster_rcnn 参数修改
https://github.com/dmlc/gluon-cv/blob/master/gluoncv/model_zoo/faster_rcnn/faster_rcnn.py 对你选用的模块,修改 ...
- Django中模型(四)
Django中模型(四) 五.创建对象 1.目的 向数据库中添加数据.当创建对象时,Django不会对数据库进行读写操作,当调用save()方法时,才与数据库交互,将对象保存到数据库中 2.注意 __ ...