#include <stdio.h>
#include<stdlib.h>//包含system()函数的头文件
void Reverse(int a[], int front, int after) {
int middle = (front + after) / 2;
int length = after - front + 1;
int temp;
for (int i = front; i <= middle; i++)
{
temp = a[i];
a[i] = a[front + after - i];//注意这里的front + after - i
a[front + after - i] = temp;
}
};
int main() {
int n;
int middle;
int a[100];
int i;
scanf("%d%d", &n, &middle);
middle %= n;//M有可能大于等于N,m = m%n;这句代码是不可缺少的
for ( i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
Reverse(a, 0, n - middle-1);
Reverse(a, n - middle, n - 1);
Reverse(a, 0, n - 1);
for (i = 0; i < n - 1; i++)
{
printf("%d ", a[i]);
}
printf("%d", a[i]);//控制格式最后一个不加空格
return 0;
}
1008 数组元素循环右移问题 (20分)
最新推荐文章于 2024-08-18 01:27:51 发布