題目連結: https://zerojudge.tw/ShowProblem?problemid=d492
# 解題思路
運用 map 將名字跟次數都記錄下,再用 setprecision()
控制位數即可!!
# 程式碼
#include <iostream> | |
#include <iomanip> | |
#include <map> | |
using namespace std; | |
int main() { | |
int T; | |
while ( cin >> T ) { | |
getchar();getchar(); | |
for ( int i = 0; i < T ; ++i ) { | |
map <string, int> numSpecies; | |
int total = 0; | |
string input; | |
while ( getline ( cin, input ) && input != "" ) | |
++numSpecies[input], ++total; | |
for ( auto it = numSpecies.begin(); it != numSpecies.end(); ++it ) | |
cout << it->first << " " << fixed << setprecision(4) << (double) it->second / total * 100 << endl; | |
cout << endl; | |
} | |
} | |
return 0; | |
} |