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

}

相关推荐
佳児素花痴╮16 分钟前
树的基础知识与查找排序算法
数据结构·算法
lueluelue477 小时前
LeetCode:链表
算法·leetcode·链表
橘子汽水16810 小时前
Leetcode 23,543合并K个升序链表,二叉树的直径
算法·leetcode·链表
huameinan狮子10 小时前
Adaboost算法原理与计算实例
算法
别怪我很水10 小时前
API中提供了VelocityTracker类用于计算触摸事件MotionEvent的速度,而其内部默认使用的方法就是最小二乘法,本 ...
算法·gitee·最小二乘法
Re.不晚13 小时前
挑战做100道力扣算法- DAY1
算法·leetcode·职场和发展
蓝悦无人机14 小时前
《Planning algorithms》读书笔记——第1章 引言
算法·读书笔记·规划算法·lavalle
2601_9555181814 小时前
C语言标准演化史:从K&R到GNU,谁才是正统?
c语言·gnu·标准演化·ansic·性能与可移植性
Sagittarius_A*14 小时前
分组密码基础(二):Feistel 结构与 DES 的设计思想
算法·信息安全·密码学·des·数论