HDU-2034 人见人爱A-B

人见人爱A-B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 96452    Accepted Submission(s): 26857


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 73 7 2 5 8 2 3 4 5 6 7 8 0 0
 
Sample Output
2 3 NULL

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[20000],b[20000];
bool c[20000]={0};
int main()
{
    vector <int> q;
    int m,n,i,j,k;
    while(cin>>n>>m,m || n)//开始没有注意m和n同时为0一直WA...
    {
    	q.clear();
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(j=0;j<m;j++)
        {
            cin>>b[j];
        }
        if(n==0) cout<<"NULL"<<endl;
    	else if(m==0) 
    	{
    		for(i=0;i<n;i++)
    		cout<<a[i]<<" ";
    		cout<<endl;
		}
		else
		{
		int num=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(a[i]==b[j])
                {
                    num++;
                    c[i]=0;
                    break;
                }
                else
                {
                    c[i]=1;
                }
            }
        }
        if(num==n) cout<<"NULL"<<endl;
        else
        {
            for(i=0;i<n;i++)
            if(c[i]) 
            {
                q.push_back(a[i]);
            }
            sort(q.begin(),q.end());
            for(i=0;i<q.size();i++)
            cout<<q[i]<<" ";
            cout<<endl;
        }        
    	}
	}       
    return 0;
}


//#include <iostream>
//#include <map>
//using namespace std;
//int main(){
//	int n,m;
//	while(cin>>n>>m,n||m){
//		int x,y,s=0;
//		map<int,int> a;
//		map<int,int>::iterator p;
//		for(int i=0;i<n;i++){
//			cin>>x;
//			a[x]=0;
//		}
//		for(int i=0;i<m;i++){
//			cin>>y;
//			if(a.find(y)!=a.end())
//				a[y]++;
//		}
//		for(p=a.begin();p!=a.end();p++){
//			if(p->second==0)
//			cout<<p->first<<" ";
//			s+=p->second;
//		}
//		if(s==n)
//			cout<<"NULL";
//			cout<<endl;
//	}
//	return 0;
//}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值