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

}

相关推荐
_dindong1 分钟前
动规:回文串问题
笔记·学习·算法·leetcode·动态规划·力扣
wangwangmoon_light29 分钟前
0.0 编码基础模板
java·数据结构·算法
共享家952733 分钟前
Leetcode刷题
算法·leetcode·职场和发展
simon_skywalker40 分钟前
第7章 n步时序差分 n步时序差分预测
人工智能·算法·强化学习
山,离天三尺三1 小时前
基于LINUX平台使用C语言实现MQTT协议连接华为云平台(IOT)(网络编程)
linux·c语言·开发语言·网络·物联网·算法·华为云
flashlight_hi2 小时前
LeetCode 分类刷题:74. 搜索二维矩阵
python·算法·leetcode·矩阵
小年糕是糕手2 小时前
【数据结构】算法复杂度
c语言·开发语言·数据结构·学习·算法·leetcode·排序算法
明天会有多晴朗2 小时前
深度剖析 C++ 之内存管理篇
c语言·开发语言·c++
程序员-King.2 小时前
day86——有效的字母异位词(LeetCode-242)
算法·字符串
potato_may2 小时前
C语言第3讲:分支和循环(上)—— 程序的“决策”与“重复”之旅
c语言·开发语言