0
点赞
收藏
分享

微信扫一扫

HDOJ--2034 人见人爱A-B

Go_Viola 2022-08-16 阅读 39

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.util.Scanner; 
public class Gether {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int m=sc.nextInt();
int ns[] = new int[n];
int ms[] = new int[m];
int cs[] = new int[n];
for(int i=0;i<ns.length;i++){
ns[i]=sc.nextInt();
}
for(int i=0;i<ms.length;i++){
ms[i]=sc.nextInt();
}
if(n==0&&m==0){
return;
}
if(n==0&&m!=0){
System.out.println("NULL");
continue;
}
if(n!=0&&m==0){
for(int i=0;i<n-1;i++){ //从小到大输出结果
for(int j=i+1;j<n;j++){
if(ns[i]>ns[j]){
ns[i]=ns[i]^ns[j];
ns[j]=ns[i]^ns[j];
ns[i]=ns[i]^ns[j];
}
}
}
for(int i=0;i<ns.length;i++){
System.out.print(ns[i]+" ");
}
System.out.println();
continue;
}
int num=0;
for(int i=0;i<ns.length;i++){
int conunt=0;
for(int j=0;j<ms.length;j++){
if(ns[i]!=ms[j]){
conunt++;
}
if(conunt==m){
cs[num]=ns[i]; //在A集合中找到一個放入cs數組中 num++;
}
}
}
for(int i=0;i<num-1;i++) { //將結果按重大到小的順序輸出
for(int j=i+1;j<num;j++){
if(cs[i]>cs[j]){
cs[i]=cs[i]^cs[j];
cs[j]=cs[i]^cs[j];
cs[i]=cs[i]^cs[j];
}
}
}
if(num==0){
System.out.println("NULL");
}else{
for(int i=0;i<num;i++){
System.out.print(cs[i]+" ");
}
System.out.println();
}
}
}
}

 

举报

相关推荐

0 条评论