1008. 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
Ideas:
n=6,m=2
You only need to store the input value at the specified location when you enter it. m=2, starting from a[2] Deposit 1 2 3 4
Then deposit in 5 6 a[0][1]
Finally output a[i]
1#include"stdio.h"2 intMainintargcChar Const*Argv[])3 {4 intn,m,i,a[ -];5scanf"%d%d",&n,&M);6 if(m>n) m=m%n;//if M>n only need to move m to N to find more than one time7 for(i=m;i<=n-1; i++) 8scanf"%d",&a[i]);9 for(i=0; i<m;i++)Tenscanf"%d",&a[i]); one for(i=0; i<n-1; i++) aprintf"%d", a[i]); -printf"%d", a[n-1] ); - return 0; the}
Pat B 1008. array element loop right shift problem (20)