{A} + {B} (TreeSet)

https://vjudge.net/contest/352887#problem/E

给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input

每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.

Output

针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.

Sample Input

1 2
1
2 3
1 2
1
1 2

Sample Output

1 2 3
1 2
import java.util.*;
public class Main {
	static Scanner sc=new Scanner(System.in);
	static Set<Integer> se=new TreeSet<Integer>();
	public static void main(String[] args) {
	    int n,m;
	    while(sc.hasNext()) {
	    	n=sc.nextInt();
	    	m=sc.nextInt();
	    	se.clear();
	    	for(int i=1;i<=n;i++) se.add(sc.nextInt());
	    	for(int i=1;i<=m;i++) se.add(sc.nextInt());
	    	boolean first=true;
	    	for(Integer x:se) {
	    		if(first==false) {
	    			System.out.print(" "+x);
	    		}
	    		else {
	    			first=false;
	    			System.out.print(x);
	    		}
	    	}
	    	System.out.println();
	    }
	}
}
发布了428 篇原创文章 · 获赞 55 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_42936517/article/details/104065856