poj 3349 数的hash

本文介绍了一种使用哈希表解决雪花唯一性问题的方法。采用除留余数法进行哈希运算,并利用链表法处理哈希冲突,确保每个雪花的六边形特征能够被正确记录并比较。

      很简单的hash题。   除留余数法。处理冲突的方法我用的是链表法。做得很顺利。一次性通过。哈哈~

#include <iostream>
#include <fstream>
#include <memory.h>

using namespace std;

#define prime 50021

int a[7];

struct hash{
bool used;
int arm[6];
hash* next;
hash(){used=false; next=NULL;}
}Hash[100001];

int main()
{
int n,i,j,sum;
bool found=false,find;
//freopen("acm.txt","r",stdin);
scanf("%d",&n);
while(n--)
{
sum=0;
for(i=0; i<6; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
sum=sum%prime; //除留余数
if(Hash[sum].used==false) //没有重复
{
for(i=0; i<6; i++)
{
Hash[sum].arm[i]=a[i];
}
Hash[sum].used=true;
}
else //有冲突
{
//比较
int count=0;
for(i=0; i<6; i++)
{
find=false;
for(j=0; j<6; j++)
{
if(Hash[sum].arm[i]==a[j])
{
count++;
find=true;
break;
}
else
continue;
}
if(find==false)
{
break;
}
}
if(count==6) //相同
{
printf("Twin snowflakes found.\n");
found=true;
break;
}
else //不同 放入链表中
{
hash *h=new hash();
for(i=0; i<6; i++)
{
h->arm[i]=a[i];
}
Hash[sum].next=h;
}
}
}
if(found==false)
{
printf("No two snowflakes are alike.\n");
}
return 0;
}

 

转载于:https://www.cnblogs.com/Jason-Damon/archive/2012/04/04/2432166.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值