2020年9月2日 星期三

10226 - Hardwood Species

解題心得:
處理資料輸入卡住好久。
用map的好處是很方便計次,也不需要額外排序。


程式碼:
#include <iostream>
#include <map>
#include <iomanip>
using namespace std;

int main()
{
    int n;
    string s;
    cin>>n;
    getchar();
    getchar();
    while(n--)
    {
        map<string,int> mp;
        map<string,int>::iterator it;
        int total=0;
        while(getline(cin,s)&&s!="")
        {
            mp[s]++;
            total++;
        }
        for(it=mp.begin();it!=mp.end();it++)
        {
            cout<<it->first<<" "<<fixed<<setprecision(4)<<(double)it->second/total*100<<endl;
        }
        if(n!=0)
            cout<<endl;
    }
    return 0;
}

沒有留言:

張貼留言