C语言编程,求字符串的hash值(散列值)

对于形如abcd...n的字符串,求字符串散列值((((a*31 + b) * 31 + c) * 31)+...)*31 + n。
输入: 字符串,如abcd

输出: 字符串对应的hashcode值,是一个整数
样例输入: abc
样例输出: 96354
答案提示: hashcode可能超过32位整数最大值,考虑使用64位整数
注意:程序从标准输入 stdin('Standard Input')获取输入,并将输出结果输出到标准输出stdout('Standard Output')。

#include<stdio.h>

int main(){
    char s[256];
char *p;
unsigned long long int h = 0;

scanf("%s", s);
for(p=s; *p; p++){
h = h*31 + *p;
}
printf("%llu", h);
}

温馨提示:答案为网友推荐,仅供参考