hash function 字符串哈希函数

#include <stdio.h>

int hash(const char *str) {
  int sum = 0;
  for (int i=0;;i++) {
    if (str[i] == '\0') break;
    sum += ((int)str[i] * 10 + i);
  }

  return sum%2069;
}


int  main(int argc, char **argv) {
  if (argc < 2) {
    printf("usage: a.out string\n");
  } else {
    printf("%d\n", hash(argv[1]));
  }

  return 0;
}

 

posted @ 2019-03-15 17:18  SKY_VIEW  阅读(262)  评论(0编辑  收藏  举报