哈希技术
#include #include #include #define maxn 1003typedef struct{ int x; int y;}Node;Node sq[1003];typedef struct THashTable{ int x,y; THashTable* next;}HashTable;HashTable* hash[maxn];void InsertHT(int i)//构建哈希表{ int key=(sq[i].x)*(sq[i].x)+(sq[i].y)*(sq[i].y);//关键字key int temH=key%maxn; if(!hash[temH]) { HashTable* tem=(HashTable*) malloc(sizeof(HashTable));//易忘点 tem->x=sq[i].x; tem->y=sq[i].y; tem->next=0; hash[temH]=tem; } else { HashTable* tem=hash[temH]; while(tem->next) { tem=tem->next; } tem->next=(HashTable*) malloc(sizeof(HashTable));//不要忘了这一步 tem->next->x=sq[i].x; tem->next->y=sq[i].y; tem->next->next=0; }}int find(int xx,int yy){ int key=xx*xx+yy*yy;//关键字key int temH=key%maxn; if(!hash[temH]) return 0;//就没有这个同义词 HashTable* tem=hash[temH]; while(tem)//易错点 { if(tem->x==xx&&tem->y==yy) return 1; tem=tem->next; } return 0;}int main(){ int n; while(scanf("%d",&n)&&n!=0) { memset(hash,0,sizeof(hash)); int i; int tot=0; for(i=0;i