手机尾号hash算法

来源:互联网 发布:显示屏改字软件 编辑:程序博客网 时间:2024/06/10 03:08
//经典算法unsigned BKDRHash(char *str){    unsigned    seed = 131; // 31 131 1313 13131 131313 etc..    unsigned    hash = 0;    while(*str)        hash = hash * seed + (*str++);    return hash;}//改进算法unsigned BKDR_Hash(unsigned suffix, unsigned slot){    unsigned    hash = 0;    int     i = 0;    char    str[21]; //号码最大长度    snprintf(str, sizeof(str), "%d", suffix);    for(i = 0; str[i] != '\0'; i++)        hash = hash * 131+ (str[i]);    return hash % slot;}

经过各省实际统计情况来看,除了尾号为4的号码较少外,其他尾号相差不多,所以采用尾号后4位来进行hash

注意:slot范围要小于131,或者可以使用1313作为种子

0 0