【題解】ZeroJudge d492: 10226 – Hardwood species

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d492
【解題想法】利用 STL map 自動對 key 排序。

#include <iostream>
#include <iomanip>
#include <map>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    string s;
    cin >> n;
    getline(cin, s); //clear buffer
    getline(cin, s); //blank line
    while (n--){
        map <string, int> mp;
        int sum = 0;
        while (getline(cin, s) && s != ""){
            mp[s]++;
            sum++;
        }
        for (auto i: mp){
            cout << i.first << " " <<
            fixed << setprecision(4) << (double)i.second / sum * 100 << "\n";
        }
        cout << "\n";
    }
    return 0;
}
分享本文 Share with friends