很简单的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;
}