题目描述:
一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M ⋯AN−1 A0A1⋯AN−M−1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?
输入格式:
每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。
输出格式:
在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4
AC的代码:
// Date:2020/04/30
// Author:xiezhg5
#include <stdio.h>
int ArrayShift(int a[],int n,int m);
int main(void) {
int a[100];
int i,m,n;
scanf("%d %d",&n,&m);
for(i=0; i<n; i++)
scanf("%d",&a[i]);
ArrayShift(a,n,m);
//注意输出格式(有空格)
for(i=0; i<n; i++) {
if(i!=0)
printf(" ");
printf("%d",a[i]);
}
return 0;
}
int ArrayShift(int a[],int n,int m) {
int i,j;
m=m%n; //避免出现m比n大重复交换的情况
int temp[n];
for(i=0; i<n; i++)
temp[i]=a[i];
for(j=0; j<n; j++) {
if(j>=(n-m))
a[j+m-n]=temp[j];
else
a[j+m]=temp[j];
}
}