백준 25314번(브론즈5) 문제를 풀며 문제발생

 

 

 


 

 

문자열을 반복하는 과정에서 메서드를 찾게 되었다.

 

.repeat(num> => num 안에는 숫자를 넣으면 된다.

System.out.println("long ".repeat(N / 4) + "int");

 

 

https://learn.microsoft.com/en-us/dotnet/api/java.lang.string.repeat?view=net-android-34.0

 

String.Repeat(Int32) Method (Java.Lang)

Returns a string whose value is the concatenation of this string repeated count times.

learn.microsoft.com

 

package Bronze5;

import java.util.Scanner;

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

        int mul = num / 4;
        String plusNum = "long ";
        if(num <= 4){
            System.out.println("long int");
        }
        else if(num > 4){
            System.out.println(plusNum.repeat(mul) + "int");
        }
    }
}

 

이렇게 짜 보았는데 코드가 지저분한것 같아서

챗GPT에게 AS요청

 

import java.util.Scanner;

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

        System.out.println("long ".repeat(N / 4) + "int");
    }
}

 

다시 정리

 

package Bronze5;

import java.util.Scanner;

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

        System.out.println("long ".repeat(N / 4) + "int");
    }
}

 

출력

 

 

'Java > 백준을 풀며' 카테고리의 다른 글

문자열연결 charAt(index)  (0) 2025.09.15
BigInteger  (1) 2025.08.28

+ Recent posts