백준 No Brainer

KIMYEONGJUN·2024년 12월 25일
0
post-thumbnail

문제

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

The first line contains a single integer n indicating the number of data sets.
The following n lines each represent a data set.
Each data set will be formatted according to the following description:
A single data set consists of a line "X Y", where X is the number of brains the zombie eats and Y is the number of brains the zombie requires to stay alive.

For each data set, there will be exactly one line of output.
This line will be "MMM BRAINS" if the number of brains the zombie eats is greater than or equal to the number of brains the zombie requires to stay alive.
Otherwise, the line will be "NO BRAINS".

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

BufferedReader를 통해 첫 번째 입력 줄에서 데이터 세트의 수 n읽어온다.
StringBuilder는 결과를 효율적으로 저장하기 위해 사용된다.
for문 사용하여 n개의 데이터 세트를 반복 처리한다.
각 데이터 세트는 공백으로 구분된 두 개의 정수X(좀비가 먹는 뇌의 수)와 Y(생존을 위해 필요한 뇌의 수)로 구성된다.
조건문을 통한 출력 결정
조건문을 사용하여 X가 Y보다 작은 경우 "NO BRAINS"를 StringBuilder에 추가한다.
그렇지 않은 경우 (즉, X >= Y인 경우) "MMM BRAINS"를 추가한다.
모든 결과를 한 번에 출력한다.
sb.toString()을 통해 StringBuilder에 저장된 문자열을 가져와 출력한다.

코드로 구현

package baekjoon.baekjoon_25;

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

// 백준 4562번 문제
public class Main879 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int n = Integer.parseInt(br.readLine());

        for(int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int X = Integer.parseInt(st.nextToken());
            int Y = Integer.parseInt(st.nextToken());

            if(X < Y) {
                sb.append("NO BRAINS\n");
            } else {
                sb.append("MMM BRAINS\n");
            }
        }

        System.out.print(sb.toString());
        br.close();
    }
}

마무리

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

profile
Junior backend developer

0개의 댓글

관련 채용 정보