본문 바로가기
Study or Lecture/항해 99클럽 코딩테스트 스터디

항해 99클럽 코딩테스트 스터디 6일차 TIL (비기너)

by yoondoo 2024. 11. 2.
728x90

 

<오늘의 학습 키워드>

- map

<오늘의 문제>

제목 : 백준 브론즈2 27160 할리갈리

 

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

<풀이>

정답은 더보기

더보기
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main {

    private static Map<String, Integer> map = new HashMap<>();

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int cnt = Integer.parseInt(br.readLine());

        for (int i = 0; i < cnt; i++) {
            String[] str = br.readLine().split(" ");

            if (!map.containsKey(str[0])) {
                map.put(str[0], Integer.parseInt(str[1]));
            } else {
                int total = map.get(str[0]);
                map.put(str[0], total + Integer.parseInt(str[1]));
            }
        }

        if (map.containsValue(5)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

 

- 입력받을 갯수 만큼 for 반복문

- 공백을 기준으로 str[0]에는 과일의 이름, str[1]에는 과일의 갯수

- str[0](과일의 이름)을 기준으로 map에 존재하고 있지 않다면, (key:과일의 이름, value:과일의 갯수) 그대로 map에 put한다.

- 만약, if(방금 입력받은 str[1](과일의 이름)이 이미 존재하고 있다면) -> (key:과일의 이름, value:원래 있던 갯수 + str[1]의 갯수) 그대로 map에 put한다.

<결론, 주의할점>

- map을 사용하지 않고 한 번 풀어보자.

반응형

댓글