Topic:
Ideas:
The entire array is reversed first, then the first M elements of the array and the subsequent elements are reversed respectively.
Code:
1 /*2 1008. Loop right shift of array elements3 */4 5#include <stdio.h>6#include <string.h>7#include <math.h>8#include <stdlib.h>9#include <time.h>Ten One #defineMAXN 101 A intP[MAXN] = {0}; - - //Reverse Position the voidSwapintAintb) { - inti; - for(I=a; i<= (a+b)/2; ++i) { - inttemp =P[i]; +P[i] = p[a+b-i]; -P[a+b-i] =temp; + } A } at - intMain () { - intN, M, I, flag=0; -scanf"%d%d", &n, &M); -M%= N;//M may be greater than N - for(i=0; i<n; ++i) { inscanf"%d", &p[i]); - } to if(M! =0) {//M is 0 no need to move +Swap0, N-1);//invert the entire array -Swap0, M-1);//Inverse array of first M elements theSwap (M, n1);//inverse array of elements behind * } $ for(i=0; i<n; ++i) {//output by FormatPanax Notoginseng if(flag) { -printf" "); the } +printf"%d", P[i]); AFlag =1; the } + - return 0; $}
1008. Loop right shift of array elements