Hdu1412 (set......Java)

给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input

每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.

Output

针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.

Sample Input

1 2
1
2 3
1 2
1
1 2

Sample Output

1 2 3
1 2

大二上不学好大二暑假在学Java  心塞塞

代码:

import java.util.*;
public class Main {
	static Scanner sc= new Scanner(System.in);
	static Set<Integer> st1 = new TreeSet<Integer>();
	public static void main(String[] args) {
		int n, m;
		while(sc.hasNext()) 
		{
			n = sc.nextInt();
			m =sc.nextInt(); 
			for(int i = 1 ;i <= n;i++)
				st1.add(sc.nextInt());
			for(int i = 1 ;i <= m;i++)
				st1.add(sc.nextInt());
			int cnt = 0;
			int over = st1.size();
			for(Iterator it = st1.iterator();it.hasNext();) {
				++cnt;
				System.out.print(it.next());
				if (cnt != over) 
					System.out.print(" ");
			}
			System.out.println();
			st1.clear();
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值