PTA 數據結構與算法題目集(中文)7-14 電話聊天狂人 (25分) 題解


源代碼:https://github.com/yunwei37/myClassNotes
還有不少數據結構和算法相關的筆記以及pta題解哦x


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

輸入格式:

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

輸出格式:

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

輸入樣例:

4
13005711862 13588625832
13505711862 13088625832
13588625832 18087925832
15005713862 13588625832

輸出樣例:

13588625832 3

#include<iostream>
#include<map>
using namespace std;
//使用map in stl:第一次 
int main(){
	map<string,int> tele;
	int n,i;
	int max=1;
	string t1,t2;
	cin>>n;
	map<string,int>::iterator tele1;
	for(i=0;i<n;++i){
		cin>>t1>>t2;
		if((tele1=tele.find(t1))!=tele.end()){
			tele1->second++;
			if(tele1->second>max)
				max=tele1->second;			
		}
		else
			tele.insert(pair<string,int>(t1,1));
		if((tele1=tele.find(t2))!=tele.end()){
			tele1->second++;
			if(tele1->second>max)
				max=tele1->second;			
		}
		else
			tele.insert(pair<string,int>(t2,1));
	}
	int count=0;
	for(tele1=tele.begin();tele1!=tele.end();++tele1){
		if(tele1->second==max&&!count){
			cout<<tele1->first<<" "<<max;
			++count;			
		}

		else if(tele1->second==max)
			++count;
	}
	if(count>1)
		cout<<" "<<count;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章