백준 Identifying tea

KIMYEONGJUN·2025년 1월 11일
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

The first line contains an integer T representing the tea type (1 ≤ T ≤ 4).
The second line contains five integers A, B, C, D and E, indicating the answer given by each contestant (1 ≤ A, B, C, D, E ≤ 4).

Output a line with an integer representing the number of contestants who got the correct answer.

내가 이 문제를 보고 생각해본 부분

BufferedReader br: 입력을 읽기 위한 객체를 생성한다.
첫 번째 줄에서 실제 차 종류를 T를 읽는다.
두 번째 줄에서 참가자들의 답변을 문자열로 읽고, 공백을 기준으로 나누어 배열에 저장한다.
각 참가자의 답변이 실제 차 종류와 일치하는지 확인하고, 일치할 경우 카운트를 증가시킨다.
최종적으로 맞춘 참가자의 수를 출력한다.

코드로 구현

package baekjoon.baekjoon_25;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// 백준 11549번 문제
public class Main897 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine().trim()); // 실제 차 종류 입력

        String[] answers = br.readLine().trim().split(" "); // 참가자들의 답변 입력
        int count = 0;

        for(String answer : answers) {  // 정답 맞춘 참가자 수 계산
            if(Integer.parseInt(answer) == T) {
                count++;
            }
        }

        System.out.println(count); // 결과 출력
        br.close();
    }
}

마무리

코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.

profile
Junior backend developer

0개의 댓글