首页 > 编程语言 > 详细

算法笔记--哈希

时间:2020-01-19 21:49:57      阅读:98      评论:0      收藏:0      [点我收藏+]

问题A:谁是你的潜在朋友

#include<cstdio>
#include<cstring>
int main(){
    const int maxn = 210;
    int N, M;
    int hashTable[maxn] = {0};
    int stu[maxn] = {0};

    while(scanf("%d%d", &N, &M) != EOF){

    memset(hashTable, 0, sizeof(hashTable));

    for(int i=1; i <= N; i++){
        int P;
        do{
        scanf("%d", &P);
        stu[i-1] = P;
        hashTable[P] ++;
        }while(P < 1 || P > M);
    }


    for(int i=1; i <= N; i++){
        int k = stu[i-1];
        if(hashTable[k] >= 2){
            printf("%d\n", hashTable[k]-1);
        }else{
            printf("BeiJu\n");
        }
    }}

    return 0;
}

注意:codeup是多点测试,所以每一次hash表得初始化为0,否则会一直出错的啊!!!

技术分享图片

 因为一行出错的心态崩 /(ㄒoㄒ)/~~,hash表初始化建议:

#include<cstring>
memset(hashTable, 0, sizeof(hashTable));

算法笔记--哈希

原文:https://www.cnblogs.com/zgqcn/p/12215580.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!