哈希算法 c语言

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// 哈希函数

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;

}

相关推荐
今儿敲了吗7 小时前
01|多项式输出
c++·笔记·算法
朔北之忘 Clancy7 小时前
2025 年 9 月青少年软编等考 C 语言一级真题解析
c语言·开发语言·c++·学习·数学·青少年编程·题解
Xの哲學7 小时前
深入剖析Linux文件系统数据结构实现机制
linux·运维·网络·数据结构·算法
AlenTech7 小时前
200. 岛屿数量 - 力扣(LeetCode)
算法·leetcode·职场和发展
C雨后彩虹7 小时前
竖直四子棋
java·数据结构·算法·华为·面试
你怎么知道我是队长8 小时前
C语言---缓冲区
c语言·开发语言
不如自挂东南吱8 小时前
空间相关性 和 怎么捕捉空间相关性
人工智能·深度学习·算法·机器学习·时序数据库
洛生&8 小时前
Elevator Rides
算法
2501_933513049 小时前
关于一种计数的讨论、ARC212C Solution
算法
Wu_Dylan9 小时前
智能体系列(二):规划(Planning):从 CoT、ToT 到动态采样与搜索
人工智能·算法