#include<stdio.h> #define M 10000 int ELFHash(char *key){ unsigned long h = 0; unsigned long g; while(*key){ h = (h<<4) + *key++; g= h & 0xf0000000L; if(g) h ^= g >> 24; h &= ~g; } return h % M; } int main(){ char s[100]; while(scanf("%s",s)){ printf("hash %s=%d\n",s,ELFHash(s)); } return 0; }
BTW:用gcc编译C++方法 gcc -o test.out test.cpp -lstdc++
如果没有-lstdc++能编译,但无法链接接到库