zoukankan      html  css  js  c++  java
  • STL应用 set hdu 1412

    set 是一个使用红黑树存储键值的结构体,存储内容没有重复,如果多次插入重复的内容,只存储一次
    常用的函数有
    size() empty() clear()
    begin() end() erase(x) erase(*it) find() count()
    lower_bound() 返回大于等于指定值的元素位置
    upper_bound() 返回大于指定值的元素位置

    这里以HDU 1412 为例讲解set的使用

    给你两个集合,要求{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
    

    解法

    // ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    
    #include <iostream>
    #include <set>
    using namespace std;
    
    
    int n, m;
    
    int main()
    {
    	while (~scanf("%d%d",&n,&m)) {
    		set<int> A;
    		int t;
    		for (int i = 0; i < n; i++) {
    			cin >> t;
    			A.insert(t);
    		}
    		for (int i = 0; i < m; i++) {
    			cin >> t;
    			A.insert(t);
    		}
    
    		for (auto it = A.begin(); it != A.end(); it++) {
    			if ( it != A.begin()) { cout << " "; }
    			cout << *it;
    		}
    		cout << endl;
    	}
    
    	return 0;
    }
    
     
    
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    HTML5 闹钟例子程序
    程序员书籍,你值得收藏
    mybatis入门案例测试常见问题以及解决方法
    jquery对Select标签的操作
    Linux下mysql整库备份
    Windows 命令提示符下查看 apache 错误的方法
    将 DataTable 转化为 Excel Xml 格式供下载
    Infragistics netadvantage UltraGrid (UltraWinGrid) 编程手记
    报表设计技巧交叉报表模板
    Gentle.NET Users' Guide
  • 原文地址:https://www.cnblogs.com/itdef/p/15085977.html
Copyright © 2011-2022 走看看