电话聊天狂人(25 分)

来源:互联网 发布:u盘如何恢复数据 编辑:程序博客网 时间:2024/04/30 02:49

给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人。

输入格式:

输入首先给出正整数N105),为通话记录条数。随后N行,每行给出一条通话记录。简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔。

输出格式:

在一行中给出聊天狂人的手机号码及其通话次数,其间以空格分隔。如果这样的人不唯一,则输出狂人中最小的号码及其通话次数,并且附加给出并列狂人的人数。

输入样例:

413005711862 1358862583213505711862 1308862583213588625832 1808792583215005713862 13588625832

输出样例:

13588625832 3
#include <bits/stdc++.h>#define KEYLENGTH 15typedef char ElementType[KEYLENGTH+1];typedef int Index;typedef struct LNode *PtrToLNode;struct LNode{    ElementType Data;    PtrToLNode Next;    int cnt;};typedef PtrToLNode Position;typedef PtrToLNode List;typedef struct TblNode *HashTable;struct TblNode{    int TableSize;    List Heads;};typedef struct Result{    ElementType MinData;    int sum;} Result;int GetTableSize(int n){    int i, j;    for(i = n; ; ++i)    {        for(j = 2; j * j <= i; ++j)            if(i % j == 0)                break;        if(j * j > i)            return i;    }}Index Hash(ElementType Key, int TableSize ){    return (atoi(Key + 6))%TableSize; //Key 取第六位到最后一位,全取的话太长}PtrToLNode GetMax(HashTable H){    int Max = 0;    PtrToLNode mmax = NULL;    for(int i = 0; i < H->TableSize; ++i)    {        PtrToLNode  temp = H->Heads[i].Next;        while(temp != NULL)        {            if(Max < temp->cnt)            {                Max = temp->cnt;                mmax = temp;            }            temp = temp->Next;        }    }    return mmax;}PtrToLNode Find(HashTable H, ElementType key){    int hvalue = Hash(key, H->TableSize);    PtrToLNode  temp = H->Heads[hvalue].Next;    while(temp != NULL)    {        if(strcmp(temp->Data, key) == 0)            return temp;        temp = temp->Next;    }    return NULL;}void Insert(HashTable &H, ElementType key){    int hvalue = Hash(key, H->TableSize);    PtrToLNode  temp = Find(H, key);    if(temp != NULL)    {        ++temp->cnt;    }    else    {        PtrToLNode ins = (PtrToLNode)malloc(sizeof(struct LNode));        strcpy(ins->Data, key);        ins->cnt = 1;        ins->Next = H->Heads[hvalue].Next;        H->Heads[hvalue].Next = ins;    }}HashTable BuildTable(){    int n;    scanf("%d", &n);    HashTable H = (HashTable)malloc(sizeof(struct TblNode));    H->TableSize = GetTableSize(2 * n);    H->Heads = (PtrToLNode)malloc(sizeof(struct LNode) * (H->TableSize));    char key1[KEYLENGTH - 1], key2[KEYLENGTH - 1];    for(int i = 0; i < H->TableSize; ++i)    {        H->Heads[i].Next = NULL;    }    for(int i = 0; i < n; ++i)    {        scanf("%s %s", key1, key2);        Insert(H, key1);        Insert(H, key2);    }    return H;}void Solve(HashTable H){    PtrToLNode Max = GetMax(H);    Result re;    strcpy(re.MinData, Max->Data);    re.sum = 0;    for(int i = 0; i < H->TableSize; ++i)    {        PtrToLNode temp = H->Heads[i].Next;        while(temp != NULL)        {            if(temp->cnt == Max->cnt)            {                ++re.sum;                if(strcmp(re.MinData, temp->Data) > 0)                    strcpy(re.MinData, temp->Data);            }            temp = temp->Next;        }    }    printf("%s %d", re.MinData, Max->cnt);    if(re.sum > 1)        printf(" %d", re.sum);    printf("\n");}int main(){    int n;    HashTable H;    H = BuildTable();    Solve(H);    return 0;}