11-散列1 電話聊天狂人 (25 分)

給定大量手機用戶通話記錄,找出其中通話次數最多的聊天狂人。

輸入格式:

輸入首先給出正整數N(≤),為通話記錄條數。隨后N行,每行給出一條通話記錄。簡單起見,這里只列出撥出方和接收方的11位數字構成的手機號碼,其中以空格分隔。

輸出格式:

在一行中給出聊天狂人的手機號碼及其通話次數,其間以空格分隔。如果這樣的人不唯一,則輸出狂人中最小的號碼及其通話次數,并且附加給出并列狂人的人數。

輸入樣例:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832

輸出樣例:

13588625832 3
//2,3測試點未過
#include<iostream>
#include<map>
using namespace std;
const int maxn = 1000100;int main(){int n;//scanf("%d",&n);cin >> n;map<string,int> mp; string s1,s2;for(int i = 0; i < n; i++){cin >> s1 >> s2;++mp[s1];++mp[s2];}int cnt = 0;//通話最多次人數int time = -1;//通話最多次的時間string s;//通話最多次最小標號 map<string,int>::iterator it;for(it = mp.begin(); it != mp.end(); it++){if(it->second > time){cnt = 1;time = it->second;s = it->first;}else if(it->second == time){cnt++;if(s < it->first) s= it->first;}}cout << s << " " << time;if(cnt > 1) cout << " " << cnt << endl;return 0;
}

https://blog.csdn.net/xijujie/article/details/53224218

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 12typedef struct ListNode *Position;
typedef struct HTable *HashTable;
struct ListNode {char data[N];int count;Position next;
};
struct HTable {Position list;int size;
};
HashTable CreatTable(int n);
void Insert(HashTable H, char *key);
void Solve(HashTable H);
int NextPrime(int n);int main() {int i, n;char key[N];HashTable H;scanf("%d", &n);H = CreatTable(n * 2);for (i = 0; i < 2 * n; i++) {scanf("%s", key);Insert(H, key);}Solve(H);return 0;
}HashTable CreatTable(int n) {HashTable H;int i;H = (HashTable)malloc(sizeof(struct HTable));H->size = NextPrime(n);H->list = (Position)malloc(H->size*sizeof(struct ListNode));for (i = 0; i < H->size; i++) H->list[i].next = NULL;    return H;
}void Insert(HashTable H, char *key) {Position p, temp;int h;h = (atoi(key + 6)) % H->size;p = H->list[h].next;while (p && strcmp(p->data, key)) {p = p->next;}if (p) p->count++;else {temp = (Position)malloc(sizeof(struct ListNode));strcpy(temp->data, key);temp->count = 1;temp->next = H->list[h].next;H->list[h].next = temp;}
}void Solve(HashTable H) {int i, max = 0, num;char min[N];Position p;for (i = 0; i < H->size; i++) {p = H->list[i].next;while (p) {if (p->count > max) {max = p->count;strcpy(min, p->data);num = 1;}else if (p->count == max) {num++;if (strcmp(p->data, min) < 0)strcpy(min, p->data);}p = p->next;}}if(num == 1)printf("%s %d\n", min, max);elseprintf("%s %d %d\n", min, max, num);}int NextPrime(int n) {int i, j;n = (n % 2) ? n + 2 : n + 1;for (i = n;; i += 2) {for (j = 3; j*j <= i && i%j; j++);if (j*j > i) break;}return i;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>#define MAXS 11
#define MAXD 5typedef struct HashEntry *List;
struct HashEntry{char PhoneNo[MAXS+1];int Cnt;List Next;
};struct HashTb1{int TableSize;List TheLists;
};
typedef struct HashTb1 *HashTable;int NextPrime(int N){int i;if(!(N%2))N++;for(;;N+=2){for(i=(int)sqrt(N);i>2;i--){if(!(N%i))break;}if(i==2)break;}return N;
}HashTable InitializeTable(int N){int i;HashTable H=(HashTable)malloc(sizeof(struct HashTb1));H->TableSize=NextPrime(N);H->TheLists=(List)malloc(sizeof(struct HashEntry)*H->TableSize);for(i=0;i<H->TableSize;i++){H->TheLists[i].Cnt=0;H->TheLists[i].Next=NULL;H->TheLists[i].PhoneNo[0]='\0';}return H;
}int Hash(int Key,int P){return Key%P;
}void InsertAndCount(char *Key,HashTable H){List Ptr,NewCell,L;L=&(H->TheLists[Hash(atoi(Key+6),H->TableSize)]);Ptr=L->Next;while(Ptr&&strcmp(Ptr->PhoneNo,Key)){Ptr=Ptr->Next;}if(Ptr){Ptr->Cnt++;    }else{NewCell=(List)malloc(sizeof(struct HashEntry));strcpy(NewCell->PhoneNo,Key);NewCell->Cnt=1;NewCell->Next=L->Next;L->Next=NewCell; }
}void Output(HashTable H){int i,MaxCnt,PCnt;List Ptr;char MinPhone[MAXS+1];MaxCnt=PCnt=0;MinPhone[0]='\0';for(i=0;i<H->TableSize;i++){Ptr=H->TheLists[i].Next;while(Ptr){if(Ptr->Cnt>MaxCnt){MaxCnt=Ptr->Cnt;strcpy(MinPhone,Ptr->PhoneNo);PCnt=1;}else if(Ptr->Cnt==MaxCnt){PCnt++;if(strcmp(MinPhone,Ptr->PhoneNo)>0){strcpy(MinPhone,Ptr->PhoneNo);}}Ptr=Ptr->Next;}}printf("%s %d",MinPhone,MaxCnt);if(PCnt>1){printf(" %d",PCnt);}
//    printf("\n");
}
int main(){int N,i;char Key[MAXS+1];HashTable H;scanf("%d",&N);H=InitializeTable(N);for(i=0;i<N;i++){scanf("%s",Key);InsertAndCount(Key,H);scanf("%s",Key);InsertAndCount(Key,H);}Output(H);    
} 

這道題留著等九月份再看把

轉載于:https://www.cnblogs.com/wanghao-boke/p/10946252.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385062.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385062.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385062.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java寫入Excel文件

首先下載jxl.jar包&#xff0c;下載地址&#xff1a;http://download.csdn.net/detail/prstaxy/4469935然后在工程文件上右鍵選Built Path—Configure Built Path切換到Libraries導入jxl.jar包。寫入Excel方法示例&#xff1a;讀取Excel見文章&#xff1a;http://blog.csdn.net…

Glib介紹

GLib是一種底層庫&#xff0c;創建GDK和GTK應用程序時該庫提供許多有用的定義和函數。包括基本類型及限制的定義、標準宏、類型轉化、字節序、存儲分配、警告和斷言、消息記錄、計時器、字符串工具、hook函數、句法掃描器、動態加載模塊和字符串自動補全&#xff0c;同時也提供…

11-散列3 QQ帳戶的申請與登陸 (25 分)

實現QQ新帳戶申請和老帳戶登陸的簡化版功能。最大挑戰是&#xff1a;據說現在的QQ號碼已經有10位數了。 輸入格式: 輸入首先給出一個正整數N&#xff08;≤&#xff09;&#xff0c;隨后給出N行指令。每行指令的格式為&#xff1a;“命令符&#xff08;空格&#xff09;QQ號碼&…

glib字符串

學過面向對象語言的同學一定都知道String類&#xff0c;一定知道這個類對字符串的操作是多麼的方便&#xff0c;但是c語言中是沒有這個類&#xff0c;甚至沒有類的概念&#xff0c;但是glib幫我們做的這個“類” GString 除了使用gchar *進行字符串處理以外&#xff0c;Glib還…

KMP 串的模式匹配 (25 分)

給定兩個由英文字母組成的字符串 String 和 Pattern&#xff0c;要求找到 Pattern 在 String 中第一次出現的位置&#xff0c;并將此位置后的 String 的子串輸出。如果找不到&#xff0c;則輸出“Not Found”。 本題旨在測試各種不同的匹配算法在各種數據情況下的表現。各組測試…

命令行工具tshark使用小記

1、目的 寫這篇博客的目的主要是為了方便查閱&#xff0c;使用wireshark可以分析數據包&#xff0c;可以通過編輯過濾表達式來達到對數據的分析&#xff1b;但我的需求是&#xff0c;怎么樣把Data部分導出來&#xff0c;因為后續的工作主要針對數據包的Data部分&#xff0c;主要…

winshark重要數據結構

說起來有一些慚愧&#xff0c;研究wireshark有一段時間了&#xff0c;但是對源代碼的分析卻至今沒有什么進展。。。 最初想要研究wireshark是因為我的開題是基于wireshark來做的。 現在有很多抓包工具&#xff0c;wireshark的優勢在于完全開源&#xff0c;分析功能強大&#xf…

C語言寫數據庫(二)

簡單的實現增刪查改的操作后&#xff0c;實現了一個先讀寫其中一個表的某兩項內容&#xff0c;再把相關字符段寫入到另外一張表中去。涉及到查詢和插入兩個步驟。 其中還涉及到漢字的讀寫和插入&#xff0c;會有字符的操作產生亂碼。所以要先保證mysql的漢字字符編碼&#xff0…

wireshark源代碼分析

各位親&#xff0c;不是我不想回復你們的問題。是我也不了解。不能誤導。希望大家相互幫助。看看能否幫那些提問的小盆友們回復一下呢&#xff1f; 這些都是轉載的&#xff0c;如果實在沒有辦法&#xff0c;可以打開鏈接到原作者哪里去提問試試看。。。 經過多次嘗試&#xf…

C語言寫數據庫(三)

遇到的問題以及解決思路方法 1.外部導入數據庫文件 進入mysql&#xff0c;創建數據庫sh_robot source /home/exbot/sh_robot.sql 查看數據庫編碼格式 show variables like “%char%”; 2.數據庫插入操作 進入相關數據庫&#xff1a;use 數據庫名&#xff1b; 查詢存在該表是否存…

Makefile(一)

在一個文件夾中建一個c文件 //main.c #include<stdio.h> int main() {printf("version 1.0");return 0; } 在當前目錄下編寫makefile文件 //makefile: test : main.o //一種依賴關系聲明&#xff0c;生成test可執行程序需要以來main.o文件gcc -o test main.…

Defunct進程 僵尸進程

在測試基于 DirectFBGstreamer 的視頻聯播系統的一個 Demo 的時候&#xff0c;其中大量使用 system 調用的語句&#xff0c;例如在 menu 代碼中的 system("./play") &#xff0c;而且多次執行&#xff0c;這種情況下&#xff0c;在 ps -ef 列表中出現了大量的 defunc…

make文件基礎用法

參照&#xff1a;https://www.jianshu.com/p/0b2a7cb9a469 創建工作目錄&#xff0c;包含一下文件 main.cperson.cb.hc.h/*** c.h ***/ //this is c.h /*** b.h ***/ //this is b.h /*** main.c ***/ #include<stdio.h> //#include"a1.h" //#include"b.h&…

一個Linux下C線程池的實現(轉)

1.線程池基本原理 在傳統服務器結構中, 常是 有一個總的 監聽線程監聽有沒有新的用戶連接服務器, 每當有一個新的 用戶進入, 服務器就開啟一個新的線程用戶處理這 個用戶的數據包。這個線程只服務于這個用戶 , 當 用戶與服務器端關閉連接以后, 服務器端銷毀這個線程。然而頻繁地…

二維數組作為函數參數

#include<stdio.h> //#include<> //二位數組作為函數參數時&#xff0c;可以不指定第一個下標 void print_buf(int (*p)[3],int a,int b) //void print_buf(int p[][3],int a,int b) {int i,j;for(i 0 ; i < a; i){for(j 0; j < b; j){printf("p[%…

mystrcat

#include<stdio.h> //如果一個數組做為函數的形參傳遞&#xff0c;那么數組可以在被調用的函數中修改 //有時候不希望這個事發生&#xff0c;所以對形參采用const參數 //size_t strlen(const char *s); //strcpy(char* s1,const char* s2); void mystrcat(char *s1,cons…

關于非阻塞的recv的時候返回的處理

注意recv&#xff08;&#xff09;如果讀到數據為0&#xff0c;那么就表示文件結束了&#xff0c;如果在讀的過程中遇到了中斷那么會返回-1&#xff0c;同時置errno為EINTR。 因此判斷recv的條件&#xff1a; 如果read返回<0 如果0 表示文件結束&…

帶參程序

windows環境 #include<stdio.h>int main(int argc, char *argv[]) {printf("argc %d\n", argc);for (int i 0; i < argc; i){printf("argv[%d] %s\n",i, argv[i]);}system("pause");return 0; } windows環境下&#xff0c;帶參函數…

Ubuntu安裝mysql步驟

1.打開終端&#xff0c;輸入&#xff1a; sudo apt-get updata 輸入root用戶密碼 2.更新完畢后&#xff0c;輸入 sudo apt-get install mysql-server ubuntu14.04安裝中間會讓你設置密碼&#xff0c;輸入密碼后點擊確認(mysql123) 3.安裝結束后&#xff0c;查看端口號是否開啟 …

Pthread創建線程后必須使用join或detach釋放線程資源

這兩天在看Pthread 資料的時候&#xff0c;無意中看到這樣一句話(man pthread_detach): Either pthread_join(3) or pthread_detach() should be called for each thread that an application creates, so that system resources for the thread can be released. …