题意是给一组长度为6的数,问里面是否有相同的。只要数相同就可以顺序可以不同。
思路,每进来一组就先排一下序。之后询问以前有没有过,如果发现重复的,后面进来的就不用管了。等数据进完输出一下有重复的就可以了。
这道题主要考的是快速查询,我用的hash,处理冲突的办法hash到位置以后往前找一直找到没存过数的地方把书存进去。
途中发现重复就返回有重复
#include<iostream>
#include<Cstdio>
#include<string>
using namespace std;
struct Node
{
int v[6];
};
Node hash[1000005];
void ShellSort(int array[],int length)
{
int i ;
int j;
int temp;
for(i=1;i<length;i++)
{
temp=array[i];
j=i-1;
while(j>=0 && array[j]>temp)
{
array[j+1]=array[j];
j=j-1;
}
array[j+1] = temp;
}
}
bool eql(int a[], int v[])
{
for(int i = 0; i < 6; i++)
if(a[i] != v[i])
return false;
return true;
}
int sum(int a[])
{
int i, tmp = 0;
tmp += (a[0] % 7 ) * 100000;
tmp += (a[1] % 7 ) * 10000;
tmp += (a[2] % 7 ) * 1000;
tmp += (a[3] % 7 ) * 100;
tmp += (a[4] % 7 ) * 10;
tmp += (a[5] % 7 );
return tmp;
}
bool getPos(int a[])
{
int i, temp;
temp = sum(a);
while(1)
{
if(hash[temp].v[0] != -1 && !eql(a, hash[temp].v))
{
temp++;
continue;
}
if(hash[temp].v[0] != -1 && eql(a, hash[temp].v))
return true;
for(i = 0; i < 6; i++)
{
hash[temp].v[i] = a[i];
}
return false;
}
return false;
}
int main()
{
int a[6], n, m, i, j;
cin >> n;
bool flag = false;
for(i = 0; i < 1000005; i++)
{
for(j = 0; j < 6; j++)
hash[i].v[j] = -1;
}
while(n--)
{
for(i = 0; i < 6; i++)
scanf("%d", &a[i]);
if(!flag)
{
ShellSort(a, 6);
if(getPos(a))
flag = true;
}
}
if(flag)
cout << "Twin snowflakes found." << endl;
else
cout << "No two snowflakes are alike." << endl;
}
。
hash函数用的是每位都模上7之后第一位成10000每位少乘10倍最后一位乘以1。