HDU2095find your present (2)【hash】

水题==

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int mod = 10005;

struct Node {
    int to, next;
}e[1000005];
int head[mod + 10];
int tot;
int a[1000005];

void add(int p, int num) {
    e[tot].to = num;
    e[tot].next = head[p];
    head[p] = tot++;
}
bool check(int num) {
    int p = num % mod;
    int cnt = 0;
    for(int i = head[p]; i; i = e[i].next) {
        if(e[i].to == num) {
            cnt++;
            if(cnt >= 2) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    int n;
    while(scanf("%d",&n) && n) {
        memset(head, 0, sizeof(head));
        tot = 1;
        for(int i = 1; i <= n; i++) {
            scanf("%d",&a[i]);
            add(a[i] % mod, a[i]);
        }
        for(int i = 1; i <= n; i++) {
            if(check(a[i])) {
                printf("%d\n", a[i]);
                break;
            }
        }
    }
    return 0;
}

 

posted @ 2014-12-09 21:12  悠悠我心。  阅读(116)  评论(0编辑  收藏  举报