# 題目: UVa 10226 - Hardwood Species
# 題目說明
計算每種樹的出現比例
INPUT:
第一行有一個整數n,代表共有幾個cases
接著空一行,之後重複讀取字串name,代表樹的名稱,直到空行
OUTPUT:
輸出樹的名稱及出現比例,順序為英文字母升冪,位數到小數點後四位
每筆資料以空行隔開
# 解題方法
由於用到cin又用到getline,所以需要先呼叫cin.ignore()清空緩衝區
用map去累積每種樹出現的數量
# 參考程式碼
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int n, total = 0;
string name;
map<string, double> tree_data;
cin >> n;
cin.ignore();
getline(cin, name);
while (n--) {
while (getline(cin, name) && name != "") tree_data[name]++, total++;
for (auto it = tree_data.begin(); it != tree_data.end(); it++)
cout << it->first << " " << fixed << setprecision(4) << it->second/total*100 << endl;
tree_data.clear();
total = 0;
}
return 0;
}