Collection (Autumn 2017 Jingdong recruit Zhenti) - This question is a pit input

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m1f2c3/article/details/100024379

Here Insert Picture Description

Note! The correct answer to use to sc.hasNext ()!

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            Set<Integer> ab = new TreeSet<>();
            for(int i = 0; i < n; ++i) ab.add(sc.nextInt());
            for(int i = 0; i < m; ++i) ab.add(sc.nextInt());
            for(int mem : ab){
                System.out.printf("%d ", mem);
            }
        }

    }
}

Here Insert Picture Description

If you do not sc.hasNext () on error

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int m = sc.nextInt();
            Set<Integer> ab = new TreeSet<>();
            for(int i = 0; i < n; ++i) ab.add(sc.nextInt());
            for(int i = 0; i < m; ++i) ab.add(sc.nextInt());
            for(int mem : ab){
                System.out.printf("%d ", mem);
            }

    }
}

Here Insert Picture Description

This test is to use input and output streams do? This test is really algorithm do?

Guess you like

Origin blog.csdn.net/m1f2c3/article/details/100024379