人见人爱A-B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 51654 Accepted Submission(s): 14567
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表示输入的结束,不做处理。
如果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
Author
lcy
本孩子 最近 刚开始接触 STL 今天晚自习的时候 被他强大的功能 可感染了!别当时看见string和动态分配数组还他妈激动。
下面是在网上找的 用到的知识点 我也算学习了。
一.count函数
algorithm头文件定义了一个count的函数,其功能类似于find。这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果。
编写程序读取一系列int型数据,并将它们存储到vector对象中,然后统计某个指定的值出现了多少次。应用范例:
//读取一系列int数据,并将它们存储到vector对象中,
//然后使用algorithm头文件中定义的名为count的函数,
//统计某个指定的值出现了多少次
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int ival , searchValue;
vector<int> ivec;
//读入int型数据并存储到vector对象中,直至遇到文件结束符
cout<<"Enter some integers(Ctrl+Z to end): "<<endl;
while(cin >> ival)
ivec.push_back(ival);
cin.clear(); // 使输入流重新有效
//读入欲统计其出现次数的int值
cout<<"Enter an integer you want to search: "<<endl;
cin>>searchValue;
//使用count函数统计该值出现的次数并输出结果
cout<<count(ivec.begin() , ivec.end() , searchValue)
<<" elements in the vector have value "
<<searchValue<<endl;
return 0;
}
普通解法:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int n,t,m,ls[101],gq[101],cnt[101];
while(cin>>n>>m,n||m)
{
int q=0;
memset(ls,0,sizeof(ls));
memset(gq,0,sizeof(gq));
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++)
cin>>ls[i];
for(int j=0;j<m;j++)
cin>>gq[j];
for(int k=0;k<n;k++)
{
for( t=0;t<m;t++)
if(ls[k]==gq[t])
break;
if(t==m)
cnt[q++]=ls[k];
}
if(q==0)
cout<<"NULL"<<endl;
else
{
sort(cnt,cnt+q);
for(int p=0;p<q;p++)
cout<<cnt[p]<<" ";
cout<<endl;
}
}
return 0;
}
set解法:
#include<set>
#include<iostream>
using namespace std;
int main()
{
int n,m,t;
set<int>s;
set<int>::iterator it;
while(cin>>n>>m,n+m)
{
while(n--)
{
cin>>t;
s.insert(t);
}
while(m--)
{
cin>>t;
if(s.count(t))
s.erase(t);
}
for(it=s.begin();it!=s.end();it++)
cout<<*it<<" ";
printf(s.size()?"\n":"NULL\n");
s.clear();
}
return 0;
}
STL相信我把你干掉!