1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <stdio.h> #define MAXN 10
int ArrayShift( int a[], int n, int m );
int main() { int a[MAXN], n, m; int i;
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]); } printf("\n");
return 0;
} Reverse(int* arr, int b, int e)
{
for(; b < e; b++, e--)
{
int temp = arr[e];
arr[e] = arr[b];
arr[b] = temp;
}
}
ArrayShift(int* arr, int N, int k)
{
k %= N;
Reverse(arr, 0, N - k - 1);
Reverse(arr, N - k, N - 1);
Reverse(arr, 0, N - 1);
}
|