[演算法練習] UVA 10420 - List of Conquests?

來源:互聯網
上載者:User

標籤: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 }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.