题解:数组右移转逆置问题。注意M可能大于N。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
void reverse(int a[], int l, int h)
{
if(l >= h)
return;
for (int i = 0; i <= (h-l)/2; i++)
{
int temp = 0;
temp = a[l+i];
a[l+i] = a[h-i];
a[h-i] = temp;
}
}
#define MAX 1000+10
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n = 0, m = 0;
int a[MAX];
cin >> n >> m;
if (m > n)
m = m%n;
for (int i = 0; i < n; i++)
cin >> a[i];
reverse(a,0,n-1);
reverse(a,0,m-1);
reverse(a,m,n-1);
for (int i = 0; i < n; i++)
{
if (i != n-1)
cout << a[i] << " ";
else
cout << a[i];
}
return 0;
}