[알고리즘] 백준 - 치킨 쿠폰

June·2021년 4월 12일
0

알고리즘

목록 보기
155/260

백준 - 치킨 쿠폰

내 풀이

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

public class baekjoon_1673 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String input = br.readLine();
            if (input == null) {
                break;
            }
            String[] inputs = input.split(" ");
            int n = Integer.parseInt(inputs[0]);
            int k = Integer.parseInt(inputs[1]);
            System.out.println(solve(n, 0, k));

        }
    }

    private static int solve(int coupon, int ordered, int k) {
        if (coupon < k) {
            return ordered + coupon;
        }
        return solve((coupon / k) + (coupon % k), ordered + (coupon - coupon % k), k);
    }
}

더 이상 쿠폰을 얻을 수 없을 때까지 재귀를 호출하는데, 쿠폰은 현재 쿠폰에서 k로 나눈 나머지와 새로 받은 쿠폰이고, 주문량은 이때까지 주문량에서 이번에 시켜먹은 양이다.

EOF를 다루는 문제다. 그냥 readLine으로 읽고 null이면 종료하면 된다.

BufferedReader & BufferedWriter

0개의 댓글