方法1:走捷径,用TreeSet就行。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
Set<Integer> set = new TreeSet<>();
int n1 = in.nextInt();
for(int i =0;i<n1;++i)
set.add(in.nextInt());
int n2 = in.nextInt();
for(int i =0;i<n2;++i){
set.add(in.nextInt());
}
for(int i:set){
System.out.print(i);
}
}
}
}
方法二:优先队列,设置一个标记记录上次打印的元素,如果相同就不打印。
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
PriorityQueue<Integer> que = new PriorityQueue(new Comparator<Integer>(){
public int compare(Integer o1,Integer o2){
return o1-o2;
}
});
int n1 = in.nextInt();
for(int i =0;i<n1;++i)
que.offer(in.nextInt());
int n2 = in.nextInt();
for(int i =0;i<n2;++i)
que.offer(in.nextInt());
int pre = que.poll();
System.out.print(pre);
while(!que.isEmpty()){
int temp = que.poll();
if(temp!=pre){
pre = temp;
System.out.print(pre);
}
}
}
}
}