运行 ❯
获取您的
自己的
网站
×
更改方向
更改主题,暗/亮
前往 Spaces
Python
C
Java
def hash_function(value): sum_of_chars = 0 for char in value: sum_of_chars += ord(char) return sum_of_chars % 10 print("'Bob' has hash code:",hash_function('Bob')) #Python
#include <stdio.h> #include <string.h> int hashFunction(const char* value) { int sum = 0; for (int i = 0; i < strlen(value); i++) { sum += (int) value[i]; } return sum % 10; } int main() { char value[] = "Bob"; printf("'Bob' has hash code: %d\n", hashFunction(value)); return 0; } // C
public class Main { public static void main(String[] args) { String value = "Bob"; System.out.println("'" + value + "' has hash code: " + hashFunction(value)); } public static int hashFunction(String value) { int sum = 0; for (int i = 0; i < value.length(); i++) { sum += value.charAt(i); } return sum % 10; } } //Java
Python 结果
C 结果
Java 结果
'Bob' 的哈希码为:5
'Bob' 的哈希码为:5
'Bob' 的哈希码为:5