Hangzhou electric acm P2019 sequence

Keywords: Java

Problem Description
There are n (n<=100) integers, which have been sorted from smallest to largest. Now another integer x is given. Please insert the number into the sequence and keep the new sequence in order.

Input
The input data contains multiple test instances, and each set of data consists of two rows, the first is n and m, and the second is a number of columns with an ordered number of n.N and M mark the end of the input data for 0 at the same time. This line will not be processed.

Output
For each test instance, the output is a number of columns after inserting a new element.

Sample Input
3 3
1 2 4
0 0

Sample Output
1 2 3 4

import java.util.Scanner;
public class P2019 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            int m=sc.nextInt();
            int[] a=new int[n];
            int[] b=new int[n+1];
            for(int i=0;i<n;i++){
                a[i]=sc.nextInt();
            }
            for(int i=0;i<n;i++){
                for(int j=i+1;j<n;j++){
                    if(a[i]>a[j]){
                    int t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                  }
                }
            }
            b[n]=m;
            for(int i=0;i<n;i++){
                b[i]=a[i];
              }
            for(int i=0;i<n+1;i++){
                for(int j=i+1;j<n+1;j++){
                    if(b[i]>b[j]){
                    int t=b[i];
                    b[i]=b[j];
                    b[j]=t;
                    }
                }
            }
            for(int i=0;i<n+1;i++){
                if(i==n){
                System.out.print(b[i]);
                }else{
                System.out.print(b[i]+" ");
                }
            }
            System.out.println();
        }
    }
}

Posted by hayw0027 on Sun, 14 Jun 2020 09:40:22 -0700