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 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
Idea: note the boundary trap, the input is infinite possible.
1#include <stdio.h>2 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/3 //reverse an array4 voidReverse (intdata [],intStartintLast )5 {6 inti;7 for(i=start;i<= (Start+last)/2; i++)8 {9 inttemp=data[start+last-i];Tendata[start+last-i]=Data[i]; Onedata[i]=temp; A } - } - the intMainintargcChar**argv) { - intArray [ the]; - inti; - intlength; + intmove; -scanf"%d%d",&length,&move); + for(i=0; i<length;i++) A { atscanf"%d",&array[i]); - } - //the reason for the error is that the number of bits moved is only assumed to be less than length -Move=move%length; -Reverse (Array,0, length-move-1); -Reverse (array,length-move,length-1); inReverse (Array,0, length-1); - for(i=0; i<length;i++) to { + if(i!=length-1) -printf"%d", Array[i]); the Else *printf"%d", Array[i]); $ } Panax Notoginseng - the + return 0; A}
View Code
PAT1008. array element loop right shift problem (20)