Exercise 2.2 Array loop left shift (20 points) Zhejiang University Edition "Data Structure (2nd edition)" topic set

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.