zoukankan      html  css  js  c++  java
  • 714 电话聊天狂人 (25分)

    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    int  max_ = 0;//每当count增加时,就判断此时增加后的count是不是最大,max_存储当前最大值
    int  f = 0;//标志有什么并列的狂人
    int N;
    int  samecount = 0;//并列狂人数
    char result[20];//狂人的电话号
    
    typedef unsigned long long  Index;
    
    Index Hash(const char* key, long  long tablesize)//《数据结构与算法分析-C语言描述》中的Hash函数
    {
        unsigned long long hashval = 0;
        while (*(key + 4) != 0)  //为什么6改为3 and 4就可以AC????
            hashval = (hashval << 5) + *key++;//*(key++)
        return hashval % tablesize;
    }
    
    struct num_node
    {
        char num[100];
        int count;
        struct num_node* next;
    };
    
    typedef struct num_node* node_ptr;
    
    node_ptr* createtable(int size)
    {
        node_ptr* H = new node_ptr[size];
        for (int i = 0; i < size; i++)
        {
            H[i] = new struct num_node;
            H[i]->count = 0;
            if (H[i] != NULL)
                H[i]->next = NULL;
        }
        return H;
    }
    
    void insert(char* str, node_ptr* H)
    {
        int flag = 0;//标志表中是否已存在此号码
        int hashval = Hash(str, 2 * N);
        node_ptr s = H[hashval];
        while (s->next != NULL)//头结点没有用来存储数据
        {
            s = s->next;
            if (!strcmp(str, s->num))
            {
                flag = 1;
                s->count++;
                if (s->count > max_)//每当count变化时判断count是不是当前最大
                {
                    strcpy(result, s->num);
                    max_ = s->count;
                }
    
                break;
            }
        }
        if (flag == 0)
        {
            node_ptr temp = new num_node;
            temp->next = NULL;
            strcpy(temp->num, str);
            temp->count = 1;
            if (temp->count > max_)
            {
                strcpy(result, temp->num);
                max_ = temp->count;
            }
            s->next = temp;
        }
    }
    
    void judge(node_ptr* H)
    {
        for (int i = 0; i < 2 * N; i++)
        {
            node_ptr s = H[i];
    
            while (s->next != NULL)
            {
                s = s->next;
                if (max_ == s->count and strcmp(result, s->num) != 0)
                {
                    f = 1;
                    samecount++;
                    if (strcmp(s->num, result) < 0)
                    {
                        strcpy(result, s->num);
                    }
                }
            }
        }
    }
    int main()
    {
        cin >> N;
        node_ptr* H = createtable(2 * N);
    
        for (int i = 0; i < N; i++)
        {
            char str1[20];
            cin >> str1;
            insert(str1, H);
            char str2[20];
            cin >> str2;
            insert(str2, H);
        }
        judge(H);//得到最大值的前提下再对有没有并列情况进行判断
        if (f == 0)
            cout << result << " " << max_;
        else if (f == 1)
            cout << result << " " << max_ << " " << samecount;
        return 0;
    }
  • 相关阅读:
    Django笔记
    在vue框架里添加bootstrap与axios
    Mysql8和Mysql5.7部署同一服务器
    docker迁入迁出mysql
    mysql导出csv
    Yearning启停脚本(开机自启)
    go 语言的基础知识
    阅读《深入理解Kafka核心设计与实践原理》第五章 日志存储
    阅读《深入理解Kafka核心设计与实践原理》第四章 主题与分区
    阅读《深入理解Kafka核心设计与实践原理》第三章 消费者
  • 原文地址:https://www.cnblogs.com/2020R/p/PTA.html
Copyright © 2011-2022 走看看