Lined Notebook

[Gold 4] [Java] 생태학 (4358 번)

by HeshAlgo

 

       programmers.co.kr/learn/courses/30/lessons/42576 -> 완주하지 못한 선수 (Level1)

 

Java Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.baekjoon.java;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
 
public class Main {
    
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        HashMap<String, Double> map = new HashMap<String, Double>();    // key : 나무 이름, value : 해당 나무 개수
        List<String> tree = new ArrayList<String>();                    // 나무 이름 저장
        
        double totalCnt = 0;        // 입력받은 총 개수
        while (scan.hasNextLine()) {
            String input = scan.nextLine();
            // 처음 들어있는 값인 경우
            if (map.get(input) == null) {
                map.put(input, 1.0);
                tree.add(input);
            } 
            // 기존에 존재할경우 갯수 증가
            else {
                map.put(input, map.get(input) + 1.0);
            }
            
            totalCnt++;
        }
        // 사전순 정렬 (오름차순)
        Collections.sort(tree);
        
        for (int i = 0; i < tree.size(); i++) {
            double cnt = map.get(tree.get(i));
            System.out.print(tree.get(i) + " ");
            System.out.printf("%.4f\n", cnt / totalCnt * 100);
        }
        
        scan.close();
        
    }
    
 
}
 
 
cs

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기