알고리즘/항해99 알고리즘

항해99 알고리즘 3일차

홍박스 2025. 1. 15. 20:49
728x90
package hanghee99;

import java.util.Scanner;

public class BOJ2675_03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        for (int i = 0; i < t; i++) {
            int r = sc.nextInt();
            String s = sc.next();;
            char[] str = s.toCharArray();

            for (int j = 0; j < str.length; j++) {
                for (int k = 0; k < r; k++) {
                    System.out.print(str[j]);
                }
            }
            System.out.println();
        }
    }
}

 

오늘의 문제는 문자열 반복 문제로 2중 for 문을 사용해서 금방 풀 수 있었다.

 

오늘은 저번의 nextInt를 활용한 문제 오류를 해결할 수 있었다.

sc.nextInt()로 정수 입력을 받은 뒤 바로 sc.nextLine()을 호출하면, 줄바꿈 문자(\n)를 처리하지 못하고 빈 문자열이 저장되는 것을 생각하여 문제를 풀면 쉽게 풀수 있을것이다.

728x90