哈希算法 c语言

#include
#include
#include

// 哈希函数
unsigned int hash_function(const char *str) {
    unsigned int hash = 0;
    while (*str) {
        hash = (hash * 31 + *str) % 1000;
        str++;
    }
    return hash;
}

int main() {
    const char *str1 = "Hello";
    const char *str2 = "World";

    unsigned int hash1 = hash_function(str1);
    unsigned int hash2 = hash_function(str2);

    printf("String: %s, Hash: %u\n", str1, hash1);
    printf("String: %s, Hash: %u\n", str2, hash2);

    return 0;
}

你可能感兴趣的:(哈希算法,散列表,算法,c语言)