Problem Solving/BOJ(백준)

[BOJ]4358번: 생태학(C++)

이진2 2021. 8. 20. 00:59

https://www.acmicpc.net/problem/4358

 

4358번: 생태학

프로그램은 여러 줄로 이루어져 있으며, 한 줄에 하나의 나무 종 이름이 주어진다. 어떤 종 이름도 30글자를 넘지 않으며, 입력에는 최대 10,000개의 종이 주어지고 최대 1,000,000그루의 나무가 주어

www.acmicpc.net

토스 코테에서 활용해야 했다고 했던 sorted map을 유의하고 푼 문제

string으로 문자열을 입력받아서 map에 저장한 뒤 각각의 비율을 출력해주는 간단한 문제엿다

파이썬으로 풀고싶었는데 개수 제한 없이 입력받는 법을 아직 모르겠다 ㅠ

#include <iostream>
#include <map>
#include <string>
using namespace std;

map<string, int> m;

int main() {
	char str[35];
	int cnt = 0;
	while (scanf(" %[^\n]s", str) != EOF) {
		string s(str); cnt++;
		if (m.find(s) != m.end()) {
			m[s]++;
		}
		else {
			m.insert(make_pair(s, 1));
		}
	}

	cout << fixed;
	cout.precision(4);
	for (auto k : m) {
		cout << k.first <<" "<< (double)(k.second * 100) / cnt << '\n';
	}
	
	return 0;
}