数据结构与算法题目集7-24——树种统计

版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/84799572

我的数据结构与算法题目集代码仓: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++解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/84799572
今日推荐