哈希算法 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;

}

相关推荐
SuperByteMaster5 小时前
freertos 任务创建方式
c语言
CodeRecycle5 小时前
Python 自动配置 pip 支持库(通过 Windows Bat 脚本)
算法
liliangcsdn5 小时前
特质偏度因子的计算示例和分析
算法
susplus6 小时前
【传感器DS18B20】51单片机上实现温度采集
c语言·51单片机·ds18b20·温度采集
荆棘鸟智能6 小时前
无人机遥感图像实时拼接算法工程实践:从特征提取到全景融合的完整技术链路
算法·无人机
6Hzlia6 小时前
【Classic 150 刷题计划】 LeetCode 58. 最后一个单词的长度 | C++ 极简反向遍历与单变量状态机
算法
AgentMaster6 小时前
企业如何应用智能客服?5 个典型场景的技术架构与实施路径
大数据·算法
程曦曦7 小时前
MySQL 生产库误删 98 张表后的时间点恢复实战:从 binlog 解析到资金对账
linux·数据结构·其他·算法·ubuntu·运维开发
Logic1017 小时前
C语言/数据结构位运算题解:异或XOR找出流水线上的“独特零件编号“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
山下梅子酒2257 小时前
洛谷-入门-B2043
c语言