#include "iostream"
#include "string.h"
using namespace std;
int main(int argc, char const *argv[])
{
int hash = 0;
char *str = "abcdef";
for(int i = 0;i < strlen(str);i++){
hash = 31*hash + (int)str[i];
}
cout<<hash<<endl;
return 0;
}
在Java中 String的hashcode()也是使用这个算法。
public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
秦九韶算法是中国南宋时期的数学家秦九韶提出的一种多项式简化算法。在西方被称作霍纳算法(Horner algorithm或Horner scheme),是以英国数学家威廉·乔治·霍纳命名的. <