首页 技术 正文
技术 2022年11月16日
0 收藏 403 点赞 3,505 浏览 1765 个字

人见人爱A-B

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2034

JAVA代码借鉴链接:https://blog.csdn.net/superbeauty/article/details/47065691

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 Input3 3 1 2 3 1 4 73 7 2 5 8 2 3 4 5 6 7 8 0 0 Sample Output2 3NULL 注:  这个题是可以用JAVA的Set类,注意输出格式,要审题,要检验结果是否符合要求。 JAVA代码:

import java.math.BigDecimal;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;import javax.swing.plaf.basic.BasicArrowButton;public class Main { public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
while(inScanner.hasNext()) {
int a = inScanner.nextInt();
int b = inScanner.nextInt();
if(a==0 && b==0) {
break;
}
else {
Set<Integer> set = new TreeSet<>(); // 要掌握。
for(int i = 0;i<a;i++) {
int x = inScanner.nextInt();
set.add(x);
}
for(int i = 0;i<b;i++) {
int x = inScanner.nextInt();
if(set.contains(x)) {
set.remove(x);
}
}
if(set.isEmpty()) {
System.out.print("NULL"); //注意格式。
}
else {
for(Integer i:set) {
System.out.print(i + " "); //注意格式。
}
}
}
System.out.println();
}
}}

也可以用C++代码:(想的有点复杂了)

思路:

这个是求A中没有B的数。此处可以用set的,只要将A中的数建立集合,然后,对B中的数进行遍历就够了。此时还会用到find,erase等函数,注意运用。此外,find函数,在set中,是在[first,last)中查询符合条件的数,如果未找到,则返回last的所在的位置。

#include <iostream>
#include<set>
using namespace std;
int main()
{
set<int> m;
int a,b,c;
while(cin>>a>>b)
{
m.clear();
if(a==&&b==)
break;
for(int i=;i<a;i++)
{
cin>>c;
m.insert(c);
}
for(int i=;i<b;i++)
{
cin>>c;
if(m.find(c)!=m.end())
m.erase(c);
}
if(m.size()==)
cout<<"NULL"<<endl;
else
{
set<int>::iterator it=m.begin();
for(;it!=m.end();it++)
cout<<*it<<" ";
cout<<endl;
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854