๊ธฐ๋ก๋ฐฉ

BOJ_9733 : ๊ฟ€๋ฒŒ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_9733 : ๊ฟ€๋ฒŒ

Soom_1n 2022. 12. 6. 18:06

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

9733๋ฒˆ: ๊ฟ€๋ฒŒ

๊ฐ๊ฐ์˜ ์ผ์„ ํ•œ ํšŸ์ˆ˜์™€ ๋น„์œจ์„ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅ์€ {Re,Pt,Cc,Ea,Tb,Cm,Ex} ์ˆœ์„œ๋Œ€๋กœ ํ•˜๋ฉฐ, ๋น„์œจ์€ ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•œ๋‹ค. ์ฃผ์–ด์ง„ ๋ชฉ๋ก์— ์—†๋Š” ์ผ์€ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ž…๋ ฅ์˜

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ํ•ด์‹œ ๋งต์„ ์ด์šฉํ•ด์„œ 7๊ฐ€์ง€ ๋ฒŒ์˜ ์ผ๊ณผ๋ฅผ ํ‚ค๋กœ ์ €์žฅํ•˜๊ณ  ๊ฐ’์„ ์นด์šดํŠธํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] arr = {"Re","Pt","Cc","Ea","Tb","Cm","Ex"};
        HashMap<String, Integer> map = new HashMap<>();
        for (String a : arr)
            map.put(a,0);

        String input = "";
        int count = 0;
        while ((input=br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(input);
            while (st.hasMoreTokens()) {
                String s = st.nextToken();
                if (map.containsKey(s))
                    map.put(s, map.get(s)+1);
                count++;
            }
        }

        for (String a : arr) {
            double result = 0.0;
            if (map.get(a)!=0)
                result = (double)map.get(a)/count;
            System.out.printf("%s %d %.2f\n", a, map.get(a), result);
        }
        System.out.printf("Total %d 1.00\n", count);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ์ด ๋Š๊ธธ๋•Œ ๊นŒ์ง€ ํ•ด์‹œ ๋งต์— ์ž…๋ ฅ๋œ ๋ฌธ์ž๋ฅผ ํ‚ค๋กœ ๊ฐ’์„ +1ํ•œ๋‹ค.
  • ์—†๋Š” ํ‚ค๋Š” ์นด์šดํŠธํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ถœ๋ ฅํ• ๋•Œ ์นด์šฐํŠธ ๊ฐ’์ด 0์ด๋ฉด ๋‚˜๋ˆ—์…ˆ ์—ฐ์‚ฐ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค. 

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ธํ…”๋ฆฌ์ œ์ด ์ฝ˜์†”์—์„œ EOF์„ ์ž…๋ ฅํ•  ์ˆ˜ ์—†์–ด์„œ ํ…Œ์ŠคํŠธ์— ์–ด๋ ค์›€์ด ์žˆ์—ˆ๋‹ค.
    • null์„ ํ™•์ธํ•˜๋ฉฐ !input.isEmpty()๋กœ ๊ฐ™์ด ํ™•์ธํ•ด์ฃผ๋ฉด ์—”ํ„ฐ๋ฅผ ํ•œ ๋ฒˆ ๋”์น˜๋Š”๊ฒƒ์œผ๋กœ ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•˜๋ฉฐ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_14425 : ๋ฌธ์ž์—ด ์ง‘ํ•ฉ  (0) 2022.12.06
BOJ_1269 : ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ  (0) 2022.12.06
BOJ_11971 : ์†๋„ ์œ„๋ฐ˜  (0) 2022.12.06
BOJ_24389 : 2์˜ ๋ณด์ˆ˜  (0) 2022.12.05
BOJ_3595 : ๋งฅ์ฃผ ๋ƒ‰์žฅ๊ณ   (0) 2022.12.05