백준 Time to Decompress

KIMYEONGJUN·2025년 1월 21일
post-thumbnail

문제

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

The first line of input contains L, the number of lines in the message.
The next L lines each contain one positive integer less than 80, followed by one space, followed by a (non-space) character.

The output should be L lines long.
Each line should contain the decoding of the corresponding line of the input.
Specifically, if line i+1 of the input contained N x, then line i of the output should contain just the character x printed N times.

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

BufferedReader를 사용하여 입력으로부터 데이터를 읽는다.
첫 번째 줄에서 총 줄 수 L을 읽고 정수로 변환한다.
메시지 처리:
for 루프를 사용하여 L개의 줄을 반복하면서 각각의 줄을 처리한다.
각 줄을 읽고 StringTokenizer를 사용하여 공백으로 나눈다.
토큰 처리:
첫 번째 토큰을 정수 N으로 변환하고, 두 번째 토큰을 문자 symbol로 가져온다.
문자 반복:
StringBuilder를 사용하여 문자열을 생성한다.
for 루프를 통해 symbol을 N번 반복하여 StringBuilder에 추가한다.
최종적으로 생성된 문자열을 출력한다.

코드로 구현

package baekjoon.baekjoon_26;

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

// 백준 17010번 문제
public class Main909 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int L = Integer.parseInt(br.readLine()); // 첫 번째 줄에서 L을 읽어옵니다.

        for(int i = 0; i < L; i++) {  // 각 줄을 처리합니다.
            String line = br.readLine();
            StringTokenizer st = new StringTokenizer(line);

            int N = Integer.parseInt(st.nextToken()); // 첫 번째 토큰을 점수로 변환
            char symbol = st.nextToken().charAt(0); // 두 번째 토큰을 가져옵니다.

            StringBuilder sb = new StringBuilder(); // 기호를 N번 반복하여 출력합니다.
            for(int j = 0; j < N; j++) {
                sb.append(symbol);
            }

            System.out.println(sb.toString());
        }

        br.close();
    }
}

마무리

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

profile
Junior backend developer

0개의 댓글