首页 > 其他 > 详细

NOJ1184 迷失的邮票 散列表

时间:2015-06-10 10:27:54      阅读:203      评论:0      收藏:0      [点我收藏+]

题意

一共收集了N张邮票,现在丢了2张,剩下N-2张…..原先收集的邮票全部是成对收集的,所以找到哪两种邮票是成单的,输出它们。(确定丢失的邮票不是同一种)

思路

因为编号比较大,可以用hash表压缩成数组可以开的下的大小。压缩直接取模就好。如果冲突就往下一个找。

代码

#include <cstdio>
#include <cstring>
#define MOD 1000007
const int maxn = 1000010;
struct node {
    int cnt;
    int num;
};
node s[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 0 ; i < n-2 ; i ++) {
        int a;
        scanf("%d",&a);//????
        int ahash = a%MOD;
        while(s[ahash].num != a && s[ahash].num != 0) {
            ahash ++;
            ahash = ahash%maxn;
        }
        s[ahash].num = a;
        s[ahash].cnt ++;
    }
    bool first = true;
    for(int i = 0 ; i < maxn ; i ++) {
        if(s[i].cnt%2) {
            if(first) {
                first = false;
                printf("%d",s[i].num);
            }else printf(" %d\n",s[i].num);
        }
    }
    return 0;
}

NOJ1184 迷失的邮票 散列表

原文:http://blog.csdn.net/area_52/article/details/46437869

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!