首页 > 代码库 > HDU2095find your present (2)【hash】

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;}

 

HDU2095find your present (2)【hash】