• hdu 2095 find your present (2)


    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=2095  

    hdu 2095 find your present (2)

    Description

    In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.

    Input

    The input file will consist of several cases. 
    Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.

    Output

    For each case, output an integer in a line, which is the card number of your present.

    Sample Input

    5
    1 1 3 2 2
    3
    1 2 1
    0

    Sample Output

    3
    2

    哈希

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<map>
    using std::map;
    using std::min;
    using std::find;
    using std::pair;
    using std::vector;
    using std::multimap;
    #define pb(e) push_back(e)
    #define sz(c) (int)(c).size()
    #define mp(a, b) make_pair(a, b)
    #define all(c) (c).begin(), (c).end()
    #define iter(c) __typeof((c).begin())
    #define cls(arr, val) memset(arr, val, sizeof(arr))
    #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    #define rep(i, n) for(int i = 0; i < (int)n; i++)
    #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
    const int N = 1000007;
    const int INF = 0x3f3f3f3f;
    int arr[N];
    struct Hash_Set {
        int tot, num[N], head[N], next[N];
        inline void init(int n) {
            tot = 0;
            rep(i, n + 1) head[i] = -1;
        }
        inline void insert(int val) {
            int u = val % N;
            num[tot] = u, next[tot] = head[u], head[u] = tot++;
        }
        inline bool find(int val) {
            int tot = 0, u = val % N;
            for(int i = head[u]; ~i; i = next[i]) {
                if(num[i] == val) tot++;
                if(tot >= 2) return true;
            }
            return false;
        }
    }hash;
    int main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w+", stdout);
    #endif
        int n;
        while(~scanf("%d", &n), n) {
            hash.init(n);
            rep(i, n) {
                scanf("%d", &arr[i]);
                hash.insert(arr[i]);
            }
            rep(i, n) {
                if(!hash.find(arr[i])) {
                    printf("%d
    ", arr[i]);
                    break;
                }
            }
        }
        return 0;
    }
    

    stl

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<map>
    using std::map;
    using std::min;
    using std::find;
    using std::pair;
    using std::vector;
    using std::multimap;
    #define pb(e) push_back(e)
    #define sz(c) (int)(c).size()
    #define mp(a, b) make_pair(a, b)
    #define all(c) (c).begin(), (c).end()
    #define iter(c) __typeof((c).begin())
    #define cls(arr, val) memset(arr, val, sizeof(arr))
    #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    #define rep(i, n) for(int i = 0; i < (int)n; i++)
    #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
    const int N = 1000007;
    const int INF = 0x3f3f3f3f;
    map<int, int> A;
    map<int, int>::iterator it;
    int main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w+", stdout);
    #endif
        int n, v;
        while(~scanf("%d", &n), n) {
            rep(i, n) {
                scanf("%d", &v);
                A[v]++;
            }
            for(it = A.begin(); it != A.end(); ++it) {
                if(it->second == 1) {
                    printf("%d
    ", it->first);
                    break;
                }
            }
            A.clear();
        }
        return 0;
    }
  • 相关阅读:
    python unittest学习4---跳过测试与预计的失败
    vue element-ui 使用 el-scrollbar监听滚动条滚动事件,处理el-tabs滚动到顶部header吸顶效果
    vue element-ui 复制文本到粘贴板
    VS Code 在HTML中生成随机文本内容
    git 添加多个远程仓库命令
    javascript——常用基础API部分方法、面试题集合
    清明时节,css3如何将网页变成灰色
    ffmpeg合并本地/线上、破解下载m3u8格式视频并转mp4格式命令
    javascript面试题
    javascript以下几种情况转换成布尔类型会得到false
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4792779.html
Copyright © 2020-2023  润新知