백준 1783 java : 구현

magicdrill·2025년 4월 2일
0

백준 문제풀이

목록 보기
579/655

백준 1783 java : 구현

처음에는 배열에 저장하려 했지만 크기가 너무 커진다.
규칙을 찾아본다.

import java.util.Scanner;

public class bj1783 {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int N = sc.nextInt();
        int M = sc.nextInt();

        System.out.println(findAnswer(N, M));

        sc.close();
    }

    public static int findAnswer(int N, int M) {
        if (N == 1) {
            return 1;
        }
        if (N == 2){
            return Math.min(4, (M + 1) / 2);
        }
        if (M < 7) {
            return Math.min(4, M);
        }
        return M - 2;
    }
}

0개의 댓글