Problem Description
參加過上個月月賽的同學一定還記得其中的一個最簡單的題目,就是{A}+{B},那個題目求的是兩個集合的並集,今天我們這個A-B求的是兩個集合的差,就是做集合的減法運算。(當然,大家都知道集合的定義,就是同一個集合中不會有兩個相同的元素,這裡還是提醒大家一下)
呵呵,很簡單吧?
Input
每組輸入數據占1行,每行數據的開始是2個整數n(0<=n<=100)和m(0<=m<=100),分別表示集合A和集合B的元素個數,然後緊跟著n+m個元素,前面n個元素屬於集合A,其余的屬於集合B. 每個元素為不超出int范圍的整數,元素之間有一個空格隔開.
如果n=0並且m=0表示輸入的結束,不做處理。
Output
針對每組數據輸出一行數據,表示A-B的結果,如果結果為空集合,則輸出“NULL”,否則從小到大輸出結果,為了簡化問題,每個元素後面跟一個空格.
Sample Input
3 3 1 2 3 1 4 7
3 7 2 5 8 2 3 4 5 6 7 8
0 0
Sample Output
2 3
NULL
import java.io.BufferedInputStream;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while (sc.hasNextInt()) {
int a = sc.nextInt();
int b = sc.nextInt();
boolean boo = true;
int aa[] = new int[a];
boolean bo[] = new boolean[a];
int bb[] = new int[b];
if (a == 0 && b == 0)
break;
for (int i = 0; i < a; i++)
aa[i] = sc.nextInt();
for (int i = 0; i < b; i++)
bb[i] = sc.nextInt();
for (int i = 0; i < aa.length; i++) {
for (int j = 0; j < bb.length; j++) {
if (aa[i] == bb[j])
bo[i] = true;
}
}
Arrays.sort(aa);
for (int i = 0; i < aa.length; i++) {
if (bo[i] != true) {
boo = false;
System.out.print(aa[i]+" ");
}
}
if (boo == true)
System.out.print("NULL");
System.out.println();
}
}
}