集合
来源:互联网 发布:淘宝商家注册流程 编辑:程序博客网 时间:2024/05/21 12:44
题目描述
给你两个集合,要求 {A} + {B}。
注:同一个集合中不会有两个相同的元素。
输入:
多组(不超过 5 组)数据。每组输入数据分为三行,第一行有两个数字 n,m($0<n,m\leq10000$),分别表示集合 A 和集合 B 的元素个数。后两行分别表示集合 A 和集合 B。每个元素为不超出 int 范围的整数,每个元素之间有一个空格隔开。
输出:
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开。
我的答案
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); //读入第一行第一个 int m = in.nextInt(); //读入第一行第二个 HashSet<Integer> hashset = new HashSet<Integer>(); for(int i = 0; i < n; i++){ hashset.add(in.nextInt()); //读入第二行数据 } for(int i = 0; i < m; i++){ hashset.add(in.nextInt()); //读入第三行数据 } Object[] arr = hashset.toArray(); int[] array = new int[arr.length]; for(int i = 0; i < array.length; i++){ array[i] = (int) arr[i]; } Arrays.sort(array); for(int i = 0; i < array.length; i++){ System.out.print(array[i]+" "); } } }}
阅读全文
0 0
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- Spring_day01
- cpp——类——虚继承——构造函数 析构函数 赋值操作符函数
- 计网相关知识简述
- storm-All grouping的应用场景1-发送信号量指令
- css3简单的方法画出一个同心圆
- 集合
- USACO-Section1.2 Transformations
- scrapy爬取昵图网图片
- 随机数的生成
- cpp——类——虚继承——作用域 名字查找
- 使用Xcode的pod插件加载第三方框架不成功
- ios textField开始输入时候 文字下移的问题
- 怎样通过微信预约瑜伽课程
- 算法导论程序28--查询二叉搜索树(Python)