[백준 실버2] 4358 : 생태학

수민이슈·2023년 9월 11일
0

[C++] 코딩테스트

목록 보기
57/116
post-thumbnail

🖊️ 문제

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


🖊️ 풀이

풀이 자체는 10초컷인데,
문제가 있었다..
백준으로 넘어오며 입출력을 제대로 안해서 생긴 .. 문제...

printf("%s %.4f", ~~~)이렇게 하려고 했는데
ios::sync_with_stdio를 하면서 그렇게 못하게 됐다
걍 저거 풀고 해도 되긴 하지만..
이건 cout관련으로 검색해봐야지

cout 소숫점 표현

	cout << fixed;
	cout.precision(4);

이렇게 해주면 소숫점 4자리수 까지 표시된다.

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

int main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	int total = 0;
	map<string, float> m;

	string input;
	while (getline(cin, input)) {
		total++;
		m[input]++;
	}
	
	cout << fixed;
	cout.precision(4);
	for (auto& tree : m) {
		cout << tree.first << " " << (float)((tree.second / total) * 100) << '\n';
	}
}

0개의 댓글