题目很简单,给出一个出席宴会的人员列表,包括国籍和姓名(姓名完全没用)。统计每个国家有多少人参加,按国家名字典排序输出。
第一行是一个整数n,表示参加人数的个数。接下来是n行,每行第一个单词为国家名称,后面为姓名。
每行包括国家名称和出席人数,将国家名称按字典排序输出。
3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna
England 1
Spain 2
1 #include<iostream> 2 #include<vector> 3 #include<cstdio> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 using namespace std; 8 9 struct Country{ 10 string name; 11 int count = 0; 12 bool operator == (const Country& b) { 13 return this->name == b.name; 14 } 15 }; 16 17 int cmp(const Country& a, const Country& b){ return a.name < b.name; } 18 19 vector<Country> Countries; 20 int main() 21 { 22 int n; 23 char str[75]; 24 scanf("%d", &n); 25 for (int i = 0; i < n; i++) 26 { 27 scanf("%s", str); //获得国家名 28 char c; 29 c = getchar(); 30 while (c != ‘\n‘&&c != EOF){ c = getchar(); } //忽视姓名 31 Country nowCt; 32 nowCt.name = str; 33 vector<Country>::iterator iter = find(Countries.begin(), Countries.end(), nowCt); 34 if (iter == Countries.end()) 35 { 36 nowCt.count = 1; 37 Countries.push_back(nowCt); 38 } 39 else 40 { 41 iter->count++; 42 } 43 } 44 sort(Countries.begin(), Countries.end(), cmp); //排序 45 for (vector<Country>::iterator it = Countries.begin(); it != Countries.end();it++) 46 { 47 cout << it->name << " " << it->count << endl; 48 } 49 return 0; 50 }
[算法练习] UVA 10420 - List of Conquests?,布布扣,bubuko.com
[算法练习] UVA 10420 - List of Conquests?
原文地址:http://www.cnblogs.com/Leroy1245/p/3818688.html