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驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    vue中dom元素和组件的获取
    Vue.js中父子组件之间的传值和传方法
    IDEA中的快捷键
    springmvc中使用controller时,跳转视图会带上外层的地址
    通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明
    vue中的组件
    vuejs
    成员变量(实例变量)&局部变量&静态变量(类变量)的区别
    代码块
    重载&重写
  • 原文地址:https://www.cnblogs.com/itdef/p/15085977.html
Copyright © 2011-2022 走看看