Each snowflake has six horns, each of which is represented by a number. Enter n snowflakes. If two snowflakes are equal, twin snowflakes found is output. Otherwise, no two snowflakes are alike.
Question: Hash
# Include <iostream> # include <cstdlib> using namespace STD; # define prime 14997 struct listnode {int A [6]; struct listnode * Next ;}; struct listnode table [prime]; bool isfindsame (INT key, int * B) {listnode * P = table [Key]. next; int I, j, CNT, s; while (P! = NULL) {for (S = 0; S <6; ++ S)/* enumeration comparison start point, a always 0-5, another constant change */{for (I = CNT = 0, j = s; CNT <6; ++ I, ++ J) // clockwise {If (j> 5) j = J % 6; If (p-> A [I]! = B [J]) break; else ++ CNT;} If (CNT = 6) return true;/* If the six corners are the same, returns true */for (I = CNT = 0, j = s; CNT <6; ++ I, -- j) // {If (j <0) j = J + 6; If (p-> A [I]! = B [J]) break; else ++ CNT;} If (CNT = 6) return true;} p = p-> next;} return false ;} void insert (INT key, int * B) {listnode * P = new (listnode); For (INT I = 0; I <6; ++ I) p-> A [I] = B [I]; P-> next = table [Key]. next; Table [Key]. next = P;} int main () {int B [6], n, hashval, ID, I, j; bool flag = false; memset (table, null, sizeof (table); scanf ("% d", & N); for (I = 0; I <n &&! Flag; I ++) {for (j = hashval = 0; j <6; j ++) {scanf ("% d", B + J ); hashval + = B [J];} id = hashval % prime; If (isfindsame (ID, B) Flag = true; else insert (ID, B );} if (FLAG) printf ("twin snowflakes found. \ n "); elseprintf (" no two snowflakes are alike. \ n "); Return 0 ;}