資料結構與演算法題目集7-24——樹種統計
阿新 • • 發佈:2018-12-05
我的資料結構與演算法題目集程式碼倉:https://github.com/617076674/Data-structure-and-algorithm-topic-set
原題連結:https://pintia.cn/problem-sets/15/problems/839
題目描述:
知識點:map集合的應用
思路:用map集合統計每個名字出現的次數
時間複雜度是O(NlogN)。空間複雜度是O(N)。
C++程式碼:
#include<iostream> #include<map> #include<string> using namespace std; int N; map<string, int> treeMap; int main(){ scanf("%d", &N); getchar(); for(int i = 0; i < N; i++){ char name[31]; scanf("%[^\n]", name); getchar(); treeMap[name]++; } for(map<string, int>::iterator it = treeMap.begin(); it != treeMap.end(); it++){ cout << it->first; printf(" %.4f%\n", it->second * 100.0 / N); } return 0; }
C++解題報告: