---
# System prepended metadata

title: Hardwood Species(UVA10226)
tags: [CPE一顆星必考題]

---

# Hardwood Species(UVA10226)
## [程式繳交區](https://hackmd.io/@Renektonn/rkoN516uyg/edit)
## 題目
[點我](https://onlinejudge.org/external/102/10226.pdf)

## 解題網站
[UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1167)
[ZJ](https://zerojudge.tw/ShowProblem?problemid=d492)

## 程式碼

```cpp=
#include <bits/stdc++.h>

using namespace std;

void func(){
    vector <string> v;
    string tree;
    while(getline(cin , tree) && tree.length() > 0){
        v.push_back(tree);
    }
    
    map <string , int> trees;
    for(int i = 0 ; i < v.size() ; i++){
        trees[v[i]] = 0;
    }
    
    for(int i = 0 ; i < v.size() ; i++){
        trees[v[i]]++;
    }
    
    double sum = 0;
    for(auto it = trees.begin() ; it != trees.end() ; it++){
        sum += (*it).second;
    }
    
    for(auto it = trees.begin() ; it != trees.end() ; it++){
        printf("%s %.4f\n" , (*it).first.c_str() , (*it).second * 100 / sum);    
    }
}

int main()
{
    int t;
    cin >> t;
    string garbage;
    getline(cin , garbage);
    getline(cin , garbage);
    
    func();
    
    for(int i = 2 ; i <= t ; i++){
        cout << endl;
        func();
    }
    
    return 0;
}
```
## 我犯的錯
1. getline()讀到'\n'，string.length()為0
2. cin後使用getline，要先用一次getline把'\n'讀走
3. 此題輸入測資數量後會空一行，所以還要再用一次getline把'\n'讀走
