99클럽 코테 스터디 6일차 TIL

Gaeng·2024년 11월 2일
post-thumbnail

할리갈리

Keyword : HashMap
해결방안
과일이름과 카운트를 받되, 같은 과일이름이라면 기존의 값과 합친다. 만약에 과일이 겹치지 않으면, 새로 입력한다.
그래서 반복문을 통해서 5이면 YES 아니면 NO로 나오게

공부
HashMap에 대해 더 자세히 공부하자.
HashMap이 익숙하지 않기에 HashMap을 익숙해지자

public class Main {
    public static void main(String[] args) throws IOException {

        Map<String, Integer> haliGali = new HashMap<String, Integer>();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            String fruit = st.nextToken();
            int fruit_count = Integer.parseInt(st.nextToken());
            if (haliGali.containsKey(fruit)) {
                haliGali.put(fruit, haliGali.get(fruit) + fruit_count);
            } else {
                haliGali.put(fruit, fruit_count);
            }
        }
        String result = "NO";
        for (Map.Entry<String, Integer> entry : haliGali.entrySet()) {
            if(entry.getValue()==5) {
                result = "YES";
            }
        }
        System.out.println(result);
    }
}
profile
문제를 해결하면서 나온 문제를 기록하는 노트

0개의 댓글