Huawei Machine test questions train in the pit

Source: Internet
Author: User

Description: Given a positive integer n for the number of trains, 0<N<10, followed by the train inbound sequence, a total of n trains, each train numbered 1-9. Requires a dictionary-ordered sequence of output train outbound

Input:

There are several sets of test cases, the first line of each group enters a positive integer n (0<n<10), and the second line consists of n positive integers, ranging from 1 to 9.

Output:

Output serial number of the train in dictionary order, each number is separated by a space, each output sequence is wrapped, see sample for details.

Sample input:
31 2 3
Sample output:
1 2 31 3 22 1 32 3 13 2 1
Idea: The input sequence is sorted by the dictionary first, and then the order of each sequence conforms to the sequence of the train exiting station. Be sure to follow the requirements of the output, more space is to kneel, don't ask me how to know ...
Import Java.util.scanner;public class Main {public static Boolean judge (int a[], int n) {//start from scratch, judging if the element below him is descending, if not, it is not correct Out of the stack sequence, if you encounter a number larger than him, then take it as the tag int tag = A[0];int p = A[0];int i = 1;for (i = 1; i < n; i++) {if (A[i] > tag) {tag = A[i];p = tag;} else if (A[i] < p) {p = a[i];} else {break;}} if (i = = N) {return true;} else {return false;}} public static int[] Reverse (int[] A, int l, int r) {int T;while (L < r) {T = a[l];a[l] = a[r];a[r] = t;l++;r--;} return A;} public static void Printzidianxu (int[] A, int n) {int p = 0;int Temp;while (true) {int flag = 0;for (int i = n-1; i > = 0; i--) {if (I-1 < 0) break;if (A[i] > A[i-1]) {p = i-1;while (i < n && a[p] < A[i]) {i++;} int Minmax = i < n? I-1: n-1;temp = A[minmax];a[minmax] = A[p];a[p] = Temp;a = Reverse (A, p + 1, n-1); if (judge (A, N)) {//////to meet the order of the train into the stack Out for (int k = 0; k < n-1; k++) {System.out.print (A[k] + "");} System.out.print (a[n-1]); System.out.println ();} flag = 1;break;}} if (flag = = 0) {break;}}} public static void Main (string[] args) {Scanner sc = new Scanner (system.in); int n = 0;while (Sc.hasnext ()) {n = sc.nextint (); int arr[] = new Int[n];for (int i = 0; i < n; i++) {Arr[i] = Sc.nextint ();} for (int k = 0; k < n-1; k++) {System.out.print (arr[k]+ "");} System.out.print (arr[n-1]); System.out.println ();p Rintzidianxu (arr, n);}}

  

Huawei Machine test questions train in the pit

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.