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

}

相关推荐
橘色的喵14 小时前
C语言面向对象范式:Nginx模块化架构的设计分析
c语言·nginx·架构·面向对象
·云扬·14 小时前
【LeetCode Hot 100】 136. 只出现一次的数字
算法·leetcode·职场和发展
Xiaochen_1214 小时前
有边数限制的最短路:Bellman-Ford 算法
c语言·数据结构·c++·程序人生·算法·学习方法·最简单的算法理解
thekenofdis19 小时前
Lua脚本执行多个redis命令提示“CROSSSLOT Keys in request don‘t hash to the same slot“问题
redis·lua·哈希算法
大胆飞猪19 小时前
递归、剪枝、回溯算法---全排列、子集问题(力扣.46,78)
算法·leetcode·剪枝
Aldrich_3221 小时前
蓝桥杯嵌入式赛道—-软件篇(GPIO输出模式配置)
c语言·vscode·stm32·单片机·嵌入式硬件·蓝桥杯
Kisorge21 小时前
【电机控制】基于STM32F103C8T6的二轮平衡车设计——LQR线性二次线控制器(算法篇)
stm32·嵌入式硬件·算法
@卞1 天前
C语言常见概念
c语言·开发语言
铭哥的编程日记1 天前
深入浅出蓝桥杯:算法基础概念与实战应用(二)基础算法(下)
算法·职场和发展·蓝桥杯
Swift社区1 天前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展