zoukankan      html  css  js  c++  java
  • 7-4 找出不是两个数组共有的元素(20 分)

    7-4 找出不是两个数组共有的元素(20 分)

    给定两个整型数组,本题要求找出不是两者共有的元素。

    输入格式:

    输入分别在两行中给出两个整型数组,每行先给出正整数N(20),随后是N个整数,其间以空格分隔。

    输出格式:

    在一行中按照数字给出的顺序输出不是两数组共有的元素,数字间以空格分隔,但行末不得有多余的空格。题目保证至少存在一个这样的数字。同一数字不重复输出。

    输入样例:

    10 3 -5 2 8 0 3 5 -15 9 100
    11 6 4 8 2 6 -5 9 0 100 8 1
    

    输出样例:

    3 5 -15 6 4 1
    
    思路:作为C++STL的练习记录一下吧,刚开始看到输出没有排序,所有用了unordered_set,但是发现这个容器说无序还真的是无序!但不是按照输入顺序来所有还是需要借助数组来保存输入序列。
    还有就是好像集合并交叉那些似乎都不太实用,性价比最高还是find();
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<unordered_set>
    
    #include<algorithm>
    using namespace std;
    int main()
    {
        unordered_set<int>se1, se2;
        int a[30], b[30];
        int n1, n2, num; cin >> n1;
        for (int i = 0; i < n1; i++)
        {
            cin >> num;
            a[i] = num;
            se1.insert(num);
        }
        cin >> n2;
        for (int i = 0; i < n2; i++)
        {
            cin >> num;
            b[i] = num;
            se2.insert(num);
        }
        
        
        unordered_set<int>::iterator iter;
        int flag = 1;
        for (int i = 0; i < n1;i++){
            
            if (se2.find(a[i]) != se2.end())
                continue;
            if (flag == 1){ cout << a[i]; flag = 0; }
            else cout << " " << a[i];
            se2.insert(a[i]);
        }
        
        for (int i = 0; i < n2;i++){
            
            if (se1.find(b[i]) != se1.end())
                continue;
            if (flag == 1){ cout << b[i]; flag = 0; }
            else cout << " " << b[i];
            se1.insert(b[i]);
        }
        if (flag != 1)cout << endl;
        
        return 0;
    }
     
  • 相关阅读:
    IOSUITextField类
    IOSUITableView设置背景图片,方式与众不同。
    IOS图标知识详情(转载自http://luoyl.info/blog/2012/03/iphoneipadicons/)
    IOSCreate UIActionSheet 'otherButtons' by passing in array
    Offset文件名(绝对偏移量)
    单例模式(Singleton)Holder
    在 spring 中一共定义了七种事务传播属性
    UML,各种关系合集
    Java,线程池,ThreadPoolExecutor
    EasyMock
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/8640765.html
Copyright © 2011-2022 走看看