[BOJ] 4358번 : 생태학

김영한·2021년 3월 4일
0

알고리즘

목록 보기
12/74

문제 링크 : 백준 4358번

[문제 접근]

map을 이용하면 간단하게 풀 수 있는 문제지만 EOF를 입력받을 때까지 입력하는 것과 소숫점 아래 4번째자리까지 출력하는 부분을 공부했다.

[소스 코드]

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>

using namespace std;
string s;
map<string, int> m;

int main(){
    double total = 0;
    while(getline(cin, s)) {
        m[s] = m[s]+1;
        total++;
    }
    cout << fixed;
    cout.precision(4);
    map<string, int>::iterator it;
    for(it=m.begin() ; it!=m.end() ; it++) {
        cout << it->first << " " << (it->second/total)*100 << "\n";
    }

    return 0;
}

0개의 댓글