1008. Loop right shift of array elements (20)
An array A contains N (n>0) integers, and each integer loop is shifted to the right by M (m>=0), and the data from a (A0 A1 ...) is not allowed to use 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 how often the program moves data, how to design the mobile
The 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 2
1 2 3 4 5 6
Sample output:
5 6 1 2 3 4
Analysis:Find the number that corresponds to the translation, subscript 0, and subscript N-1, and when the bounds are found, the number in the middle of the output
#include <iostream>#include<stdlib.h>using namespacestd;inta[ -];intMain () {intN, M; CIN>> N >>M; intFlag =0; for(inti =0; i < N; i++) Cin>>A[i]; for(inti =0; ; i++) { if((i + M)% N = =0) Flag=1; if(Flag = =1) { if((i + M)% n = = N-1) {cout<< a[i% N] <<Endl; Break; } Elsecout<< a[i% N] <<" "; }} system ("Pause"); return 0;}
Pat Serie 1008. Array elements Loop right shift problem (20)