처음에 테스트 케이스를 받아야하므로 정수형을 입력받아야한다.

그리고 나서 버퍼를 지워야하기 위해 nextLine을 사용

 

 

처음에 charAt을 사용해서 Hello라는 문구를 넣었을 때

숫자로 나왔다.

아스키 코드인 H(72) 와 o(111)의 합이 183이기 때문

 

그래서 찾아보니 문자열을 더하기 위해선 앞에 " " 를 붙여줘야 한다.

 

 

 

 

import java.util.Scanner;

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

        int T = sc.nextInt();
        sc.nextLine();

        for(int i = 0; i < T; i++){
            String str = sc.nextLine();
            System.out.println("" + str.charAt(0) + str.charAt(str.length() - 1));
        }
    }
}

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

repeat(num)  (0) 2025.09.05
BigInteger  (1) 2025.08.28

백준 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

 

백준 브론즈5를 풀다가 숫자를 나누려하는데 나누기가 되지 않는 상황 발생

 


 

그 이유는 사용가능 범위를 초과하였기 때문

 

 

 


 

 

그래서 BigInteger을 사용하여야 한다.

 

https://learn.microsoft.com/ko-kr/dotnet/api/java.math.biginteger?view=net-android-34.0

 

BigInteger 클래스 (Java.Math)

변경할 수 없는 임의 정밀도 정수입니다.

learn.microsoft.com

 

하지만 BigInteger은 산술 연산자를  사용하지 못하는데

그 이유는 객체이기 때문이다.

자바는 연산자 오버로딩을 지원하지 않는다.

 

 

따라서 우리는 이런 식으로 사용해야 한다.

 

 

 


 

 

백준에서는 클래스명을 Main으로 쓰지 않으면 컴파일 에러가 난다.

import java.math.BigInteger;
import java.util.Scanner;

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

        BigInteger n = sc.nextBigInteger();
        BigInteger m = sc.nextBigInteger();

        System.out.println(n.divide(m));
        System.out.println(n.remainder(m));
        sc.close();
    }
}

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

문자열연결 charAt(index)  (0) 2025.09.15
repeat(num)  (0) 2025.09.05

+ Recent posts