標籤:style blog color os 問題 演算法
UVA Online Judge 題目10420 - List of Conquests問題描述:
題目很簡單,給出一個出席宴會的人員列表,包括國籍和姓名(姓名完全沒用)。統計每個國家有多少人蔘加,按國家名字典排序輸出。
輸入格式:
第一行是一個整數n,表示參加人數的個數。接下來是n行,每行第一個單詞為國家名稱,後面為姓名。
輸出格式:
每行包括國家名稱和出席人數,將國家名稱按字典排序輸出。
樣本輸入:
3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna
樣本輸出:
England 1
Spain 2
代碼:(直接用STL吧。。太懶了)
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 else40 {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 }