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)(最 ...
随机推荐
- jQuery中index()方法用法实例
本文实例讲述了jQuery中index()方法用法.分享给大家供大家参考.具体分析如下: 此方法可以搜索匹配元素,并返回元素的索引值.索引值是从0开始的. 语法结构一: 当此方法没有参数的时候,返回值 ...
- [翻译] KGModal
KGModal KGModal is an easy drop in control that allows you to display any view in a modal popup. The ...
- ZT extern "C"的用法解析
extern "C"的用法解析 1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同. ...
- WebDriverException: Message: A session is either terminated or not started
错误提示: …… [debug] [XCUITest] Connection to WDA timed out[debug] [iProxy] recv failed: Operation not p ...
- Jenkins获取编译状态
背景:在通过python的API调用Jenkins,启动Jenkins的job任务时,是需要知道Jenkins的编译状态,获取编译状态为 status=server.get_build_info(jo ...
- monad重新理解
monad是高阶抽象类型: 包含类型构造器: monad抽象的核心是类型封装和类型转化(map). 实现monad的的类型必须实现(基础)类型的封装和类型转化的功能: 在此基础上实现其他的功能(基本依 ...
- 新闻cms管理系统功能介绍
一. 后台登录功能 1.后台登录 2.数据校验 3. dialog插件 4.layer插件 5. 退出登录(利用session失效) 二. 菜单管理 1.后台入口文件优化 2.公共文件引入 3.菜单的 ...
- 13.56Mhz SI522兼容MFRC522的资料以及对比性能
(13.56Mhz芯片) SI522是一颗专门替代MFRC522/FM17522,PIN对PIN 完全软硬件兼容.相对于MFRC522,SI522完全替换,不需要做任何更改,同时接受模式下功耗低10m ...
- 修改微软RDP远程桌面端口
远程桌面服务所使用的通信协议是Microsoft定义RDP(Remote Desktop Protocol)协议,RDP协议的TCP通信端口号是3389. 有时候为了安全起见,或者其他的需要,我们常需 ...
- HDU 1165 公式推导题
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1165 Eddy's research II Time Limit: 4000/2000 MS (Java/Othe ...