Fafu 1266 数数(hash应用)

来源:互联网 发布:c语言中怎么使用char 编辑:程序博客网 时间:2024/05/29 03:50

http://acm.fafu.edu.cn/problem.php?id=1266


把2^31范围内的数 %10000求得真hash值

把值存到hash链表中


#include <stdio.h>#include <string.h>struct node{int val;node * next;}hash[10000];int main(){freopen("E:\\input.txt", "r", stdin);int n;while(scanf("%d", &n) != EOF){memset(hash, 0, sizeof(hash));int hashVal;while(n--){int v;scanf("%d", &v);hashVal = v%10000;node * last = hash[hashVal].next; //savehash[hashVal].next = new node;hash[hashVal].next->val = v;hash[hashVal].next->next = last;}int m;scanf("%d", &m);while(m--){int query;scanf("%d", &query);hashVal = query % 10000;node * pointer = hash[hashVal].next;int times = 0;while(pointer != NULL){if(pointer->val == query)times++;pointer = pointer->next;}if(m != 0)printf("%d ", times);elseprintf("%d\n", times);}}return 0;}