본문 바로가기

Problem Solving/BOJ 백준

[ BOJ 백준 4358번 - 생태학 ] 해설 및 코드

 

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

 

목적

중복된 값이 존재하는 종을 입력으로 받아, 각 종을 사전순으로 비율과 함께 출력한다.

 

접근법

1. key의 자료형이 string이고, value의 자료형이 int인 map으로 입력 데이터를 처리한다.

2. 출력 형식에 유의한 처리를 별도로 해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<map>
#define endl '\n'
 
using namespace std;
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie();cout.tie();
    
    map<string,int> m;
    string species;
    int total=0;
    while(getline(cin,species)){
        ++m[species];
        ++total;
    }
    
    cout.precision(4);
    cout<<fixed;
    
    for(auto ele:m) cout<<ele.first<<" "<<((double)ele.second/total)*100<<endl;
}
 
 

 

 

 

문제 설명과 코드에 대한 피드백은 언제나 환영합니다.

 다양한 의견 댓글로 남겨주세요.