1008. Circular right shift of array elements (20)
There are N (N>0) integers in an array A. On the premise that another array is not allowed, move each integer to the right by M (M>=0) positions cyclically, that is, the data in A is replaced by ( A 0 A 1 ……A N-1 ) is transformed into (A N-M …… A N-1 A 0 A 1 …… A N-M-1 ) (the last M numbers are cyclically moved to the first M positions). If you need to consider the number of times the program moves data as little as possible, how to design the method of movement?
Input format: Each input contains a test case, the first line input N ( 1<=N<=100), M (M>=0); the second line input N integers, separated by spaces.
Output format: output the integer sequence after cyclic right shift of M bits in one line, separated by spaces, and there must be no extra spaces at the end of the sequence.
Input sample:6 2 1 2 3 4 5 6Sample output:
5 6 1 2 3 4
Program description:
1. This question can directly print out the required array elements without shifting the array
2. In order to prevent m>=n from happening, add a sentence m%=n, increase the lu The sticky
C++ code is as follows:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main(){ 5 int m,n,i; 6 cin>>n>>m; 7 m %= n; 8 int num[n]; 9 for(i=0;i<n;i++) 10 cin>>num[i]; 11 for(i=n-m;i<n;i++) 12 cout<<num[i]<<' '; 13 for(i=0;i<n-m-1;i++) 14 cout<<num[i]<<' '; 15 cout<<num[n-m-1]; 16 system("pause"); 17 }