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

}

相关推荐
keepquietl19 分钟前
MQTT示例体验(C)
c语言·开发语言
豪斯有话说1 小时前
C++_哈希表
数据结构·c++·散列表
jndingxin1 小时前
OpenCV CUDA模块光流计算-----实现Farneback光流算法的类cv::cuda::FarnebackOpticalFlow
人工智能·opencv·算法
编程绿豆侠2 小时前
力扣HOT100之栈:394. 字符串解码
java·算法·leetcode
朝朝又沐沐2 小时前
基于算法竞赛的c++编程(18)string类细节问题
开发语言·c++·算法
Wangshanjie_982 小时前
【C语言】-递归
c语言
爱coding的橙子2 小时前
每日算法刷题Day27 6.9:leetcode二分答案2道题,用时1h20min
算法·leetcode·职场和发展
GalaxyPokemon3 小时前
LeetCode - 3. 无重复字符的最长子串
算法·哈希算法·散列表
a.3023 小时前
C++ 时间处理指南:深入剖析<ctime>库
数据结构·c++·算法
亮亮爱刷题3 小时前
算法刷题-回溯
算法