【集合的简单性质】| HDU-1412 {A}+{B}
2018-03-21 20:31
363 查看
给你两个集合,要求{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
利用set的性质我们可以便易的解决这道题:1.集合的互异性 2.set容器内元素的有序性。
我们声明一个int类型的集合ans。将输入的数字都加入到ans中,集合的性质决定了其中不存在重复且自动排序,然后依次输出。
通过代码:
谢谢浏览。
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
利用set的性质我们可以便易的解决这道题:1.集合的互异性 2.set容器内元素的有序性。
我们声明一个int类型的集合ans。将输入的数字都加入到ans中,集合的性质决定了其中不存在重复且自动排序,然后依次输出。
通过代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include<cstdio> #include<iostream> #include<set> #include<iterator> using namespace std; set<int> ans; int main() { int n,m; while(cin >> n >> m) { ans.clear(); int N = n + m; int a; while(N --) { cin >> a; ans.insert(a); } int z = 0; for(set<int>::iterator it = ans.begin();it != ans.end();it ++) { if(z) { cout << " "; } else{ z = 1; } cout << *it; } cout << endl; } return 0; } |
相关文章推荐
- hdu 1412 {A} + {B}(简单的合并集合)
- Hdu 1412({A} + {B}),简单的set应用
- hdu 1412 {A} + {B}(集合合并)
- hdu 2454 简单图性质的运用
- hdu 1021 Fibonacci Again ——同余的简单性质
- HDU 6106 Classes (简单集合运算 17多校第六场)
- hdu 2035 人见人爱A^B ——同余的简单性质
- HDU 4349 Xiao Ming's Hope 简单的数学—组合数性质
- hdu 1227 简单dp+中位数的性质
- hdu_1222_欧几里得_gcd简单应用_欧几里得扩展性质
- HDU 1302 The Snail(简单数学题)
- HDU 1492 The number of divisors(约数) about Humble Numbers(数论,简单约数)
- HDU 2571-----简单的DP
- hdu 1176 简单动态规划
- GCD and LCM+hdu+利用gcd和lcm的性质
- CYC-集合视图 UICollectionView的简单使用
- hdu 3008 简单dp(背包的思想)
- hdu 4405(概率dp简单题)
- HDU-2147 kiki's game 简单博弈(*)
- Arraylist集合的简单运用