《数据结构学习与实验指导》5-3:电话聊天狂人

来源:互联网 发布:数据库基础知识点 编辑:程序博客网 时间:2024/04/30 06:11

实验内容:给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人。
输入格式:输入首先给出正整数N(≤10^5),为通话记录条数。随后N行,每行给出一条通话记录。简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。
输出格式:在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数。
测试用例:

输入 输出 4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832 13588625832 3 4
18087925832 15005713862
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832 13588625832 2 3
#include <stdio.h>#include <stdlib.h>#include <string.h>#define MaxN 100000typedef struct Node {    char value[12];    int count;    struct Node *next;} *PNode;typedef struct HashTable {    int size;    PNode arr;} *PHashTable;int N, P;int findPrime(int N);PHashTable init();int hash(char arr[]);void insert(PHashTable table, char arr[]);int findMax(PHashTable table, char *arr, int *count);int main() {    scanf("%d", &N);    P = findPrime(N);    PHashTable table = init();    int i;    char str[12];    for (i = 0; i < N; i++) {        scanf("%s", str);        insert(table, str);        scanf("%s", str);        insert(table, str);    }       int max = 0;    int same = findMax(table, str, &max);    if (same == 1) {        printf("%s %d", str, max);    } else {        printf("%s %d %d", str, max, same);    }    return 0;}int findPrime(int N) {    int num = 2 * N + 1;    while (1) {        int flag = 0;        int i;        for (i = 3; i < num; i += 2) {            if (num % i == 0) {                flag = 1;                break;            }        }        if (flag) {            num += 2;        } else {            break;        }    }    return num;}PHashTable init() {    PHashTable table = (PHashTable) malloc(sizeof(struct HashTable));    table->size = P;    table->arr = (PNode) malloc(sizeof(struct Node) * table->size);    int i;    for (i = 0; i < table->size; i++) {        table->arr[i].count = 0;        table->arr[i].next = NULL;    }    return table;}int hash(char arr[]) {    int num = 0;    int i;    int len = strlen(arr);    for (i = len - 5; i < len; i++) {        num = num * 10 + arr[i] - '0';    }    return num % P;}void insert(PHashTable table, char arr[]) {    int key = hash(arr);    if (table->arr[key].count == 0) {        strcpy(table->arr[key].value, arr);        table->arr[key].count = 1;    } else {        PNode p = table->arr + key;        PNode pp = p;        int flag = 0;        while (p != NULL && strcmp(arr, p->value) != 0) {            pp = p;            p = p->next;        }        if (p == NULL) {            PNode nn = (PNode) malloc(sizeof(struct Node));            strcpy(nn->value, arr);            nn->count = 1;            nn->next = NULL;            pp->next = nn;        } else if (strcmp(arr, p->value) == 0) {            p->count++;        }    }}int findMax(PHashTable table, char *arr, int *count) {    int same = 1;    char number[12] = "99999999999";    int max = 0;    int i;    for (i = 0; i < table->size; i++) {        if (table->arr[i].count > 0) {            PNode p = table->arr + i;            while (p != NULL) {                if (p->count > max) {                    max = p->count;                    strcpy(number, p->value);                    same = 1;                } else if (p->count == max) {                    same++;                    if (strcmp(p->value, number) < 0) {                        strcpy(number, p->value);                    }                }                p = p->next;            }        }    }    strcpy(arr, number);    *count = max;    return same;}
原创粉丝点击