The subject requires the implementation of a simple function that loops the left of an arrayA is stored inN>0) Integer to move each integer loop to the left without allowing the use of a different arrayM≥0) a position that willThe data in a is determined by (A?0??A?1???A?N−1?? ) Transformation to (A?M???A?N−1??A?0??A?1???A?m−1?? (The first M-number loop moves to the M-position of the last face ). If you still need to consider how many times the program moves data, how do you design a moving method?
Input format:
Enter the 1th line to give a positive integerN≤100) and integer m ( ≥0); the 2nd line gives n integers, separated by a space.
Output format:
In a row, the output loop moves the left m bit after the integer sequence, separated by a space, with no extra spaces at the end of the sequence.
Input Sample:
8 31 2 3 4 5 6 7 8
Sample output:
4 5 6 7 8 1 2 3
Code:
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); intn,m,t; intA[] =New int[100]; N=Sc.nextint (); M=Sc.nextint (); for(inti=0;i<n;i++) A[i]=Sc.nextint (); for(inti=1;i<=m;i++) {T=a[0]; for(intj=0;j<n-1;j++) A[j]=a[j+1]; A[n-1]=T; } System.out.print (a[0]); for(inti=1;i<n;i++) System.out.printf ("%d", A[i]); }}
Exercise 2.2 Array loop left shift (20 points) Zhejiang University Edition "Data Structure (2nd edition)" topic set