00-Self Test 3. array element loop right shift problem (20) time limit MS Memory limit 65536 KB code length limit 8000 B award Program Standard
An array A contains N (n>0) integers, and each integer loop is shifted to the right by the M (m>=0) position, and the data from a (a0a1 ...) is removed by the use of a different array. AN-1) transform to (an-m ... AN-1 A0 A1 ... AN-M-1) (the last m-number loop moves to the first m-position). If you need to consider the number of times a program moves data, how do you design a moving method?
input Format: Each input contains a test case, line 1th input n (1<=n<=100), M (m>=0), and line 2nd enter n integers, separated by a space.
output format: the sequence of integers after the output loop is shifted to the right of the M-bit in a row, separated by a space, with no extra spaces at the end of the sequence.
Input Sample:
6 21 2 3 4 5 6
Sample output:
5 6 1 2 3 4
Submit Code
Note that M can be greater than n, or equal to 0
1#include <cstdio>2#include <cstring>3#include <string>4#include <queue>5#include <cmath>6#include <iostream>7 using namespacestd;8 voidReserveint*p,intAintb) {9 if(b<a)Ten return; One intI,mid; A for(i=0; i<= (B-A)/2; i++){ -mid=p[a+i]; -p[a+i]=p[b-i]; thep[b-i]=mid; - } - } - intMain () { + //freopen ("D:\\input.txt", "R", stdin); - inti,n,m; +scanf"%d%d",&n,&m); A int*p=New int[n]; at for(i=0; i<n;i++){ -scanf"%d",&p[i]); - } -m=m%n;//key points!!!! -Reserve (P,0, N-1-m); -Reserve (p,n-m,n-1); inReserve (P,0, N-1); -cout<<p[0]; to for(i=1; i<n;i++){ +cout<<" "<<P[i]; - } thecout<<Endl; * Delete[]p; $ return 0;Panax Notoginseng}
pat00-self-Test 3. array element loop right shift problem (20)