BOJ 1476. 날짜계산

1 minute read

BOJ 1476. 날짜 계산

image image

풀이

(1 <= E <= 15) , (1 <= S <=28), (1 <= M <=19)
이므로 경우의 수는 15 * 28 * 19 = 7980으로 
1초 안에 수행할 수 있는 수 1억에 한참 못 미치는 숫자로, 여유 있게 구할 수 있으니
모든 경우의 수를 대입해 본다.

경우의 수 1~7980을 각각 E (15), S(28), M(19)로 나누었을 때 나머지를 구하여서 
입력과 같은지 확인해보면 된다.

입력받은 수에 모두 -1을 해주고
출력하기 전에 i에 +1을 해준다 (나머지 연산 예외처리 , ex) 15%15, 30%15.. 일 때 0이므로)

import java.io.IOException;
import java.util.Scanner;

class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int e = sc.nextInt() - 1;
        int m = sc.nextInt() - 1;
        int s = sc.nextInt() - 1;

        for (int i = 0; ; i++) {
            if (i % 15 == e && i % 28 == m && i % 19 == s) {
                System.out.println(i + 1);
                sc.close();
                System.exit(0);
            }
        }
    }
} 
import java.util.*
import kotlin.system.exitProcess

fun main() = with(Scanner(System.`in`)) {
    val e = nextInt() - 1
    val m = nextInt() - 1
    val s = nextInt() - 1

    var i = 0
    while (true) {
        if (i % 15 == e && i % 28 == m && i % 19 == s) {
            println(i + 1)
            exitProcess(0)
        }
        i++
    }
} 

풀이소스

github.com/beomjo/algorithm-study/commit/6b26ce6f92e3b115bc5dff1feea3691d64905959