首页 > 试题广场 >

有序序列插入一个数

[编程题]有序序列插入一个数
  • 热度指数:25644 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一个有序数字序列,从小到大排序,将一个新输入的数插入到序列中,保证插入新数后,序列仍然是升序。

输入描述:

第一行输入一个整数(0≤N≤50)。

第二行输入N个升序排列的整数,输入用空格分隔的N个整数。

第三行输入想要进行插入的一个整数。



输出描述:
输出为一行,N+1个有序排列的整数。
示例1

输入

5
1 6 9 22 30
8

输出

1 6 8 9 22 30
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @Title: 有序序列插入一个数
 * @Remark: 有一个有序数字序列,从小到大排序,将一个新输入的数插入到序列中,保证插入新数后,序列仍然是升序。
 * 输入描述:
 *      第一行输入一个整数(0≤N≤50)。
 *      第二行输入N个升序排列的整数,输入用空格分隔的N个整数。
 *      第三行输入想要进行插入的一个整数。
 *
 * 输出描述:
 *      输出为一行,N+1个有序排列的整数。
 * @Author: ijunfu
 * @Version: 1.0.0
 * @Date: 2022-03-20
 */
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        if(in.hasNextLine()) {
            int count = Integer.parseInt(in.nextLine());

            if(in.hasNextLine()) {
                List<Integer> list = Arrays.stream(in.nextLine().split(" "))
                        .map(x -> Integer.parseInt(x))
                        .collect(Collectors.toList());

                if(in.hasNextLine()) {
                    int val = Integer.parseInt(in.nextLine());

                    list.add(val);

                    Collections.sort(list);

                    String str = list.stream()
                            .map(x -> String.valueOf(x))
                            .collect(Collectors.joining(" "));

                    System.out.println(str);
                }
            }
        }


        in.close();
    }
}

发表于 2022-03-20 11:52:34 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Set<Integer> set = new TreeSet();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            set.add(sc.nextInt());
        }
        set.add(sc.nextInt());
        for (Integer i : set) {
            System.out.print(i + " ");
        }
    }
}

发表于 2021-12-05 09:24:18 回复(0)
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        TreeSet<Integer> treeSet = new TreeSet<Integer>();
        Scanner scanner = new Scanner(System.in);
        int total = scanner.nextInt();
        for(int i=0;i<total;i++){
            int data = scanner.nextInt();
            treeSet.add(data);
        }
       /* System.out.println(treeSet);*/
        int added = scanner.nextInt();
        treeSet.add(added);
        Iterator<Integer> i = treeSet.iterator();
        while (i.hasNext()){
            System.out.printf("%d ",i.next());
        }
    }
}

发表于 2021-11-11 22:43:12 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] num = new int[n + 1];
        for (int i = 0; i < n+1; i++) {
            num[i] = scanner.nextInt();
        }
        Arrays.sort(num);
        for (int j = 0; j <= n; j++) {
            System.out.print(num[j] + " ");
        }
    }
}


发表于 2021-10-22 17:44:45 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>(n);
        for(int i = 0;i<n;i++){
            list.add(sc.nextInt());
        }
        list.add(sc.nextInt());
        Collections.sort(list);
        for(Integer i:list){
            System.out.print(i+" ");
        }
    }
}

发表于 2021-07-19 18:12:39 回复(0)
import java.util.*;

public class Main {
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        int[] arr1 = new int[n];

        String[] str = sc.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            arr1[i] = Integer.parseInt(str[i]);
        }
        int m = Integer.parseInt(sc.nextLine());
        int[] arr2 = new int[n + 1];
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
        arr2[n] = m;
        Arrays.sort(arr2);
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + " ");
        }
    }
}

发表于 2021-07-13 20:50:08 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            list.add(scanner.nextInt());
        }
        int m = scanner.nextInt();
        list.add(m);
        Collections.sort(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");
        }
    }

}

发表于 2020-12-30 15:39:41 回复(0)
发现没有java的解法,所以补充一下。。。
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.io.IOException;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] score = new int[n+1];
        for (int i = 0; i<n; i++){
            score[i] = sc.nextInt();
        }
        int number = sc.nextInt();

        boolean flag = false;
        for (int i=n-1; i>=0; i--){
            if (score[i] > number)
                score[i + 1] = score[i];
            else if (score[i] <= number){
                score[i+1] = number;
                flag = true;
                break;
            }
        }

        if(!flag)
            score[0] = number;

        for (int i=0; i<n+1; i++)
            System.out.print(score[i] + " ");
        System.out.println();
    }
}


发表于 2020-06-20 14:42:38 回复(0)