알고리즘

백준 23351번 물 주기 (JAVA)

박카스마시며코딩 2023. 3. 7. 13:55

https://www.acmicpc.net/problem/23351

 

23351번: 물 주기

첫째 줄에 자연수 $N$, $K$, $A$, $B$가 공백을 사이에 두고 주어진다. ($2 \le N \le 100$, $1 \le K \le 100$, $1 \le A \times B < N$, $A$는 $N$의 약수)

www.acmicpc.net

 

package BOJ.simulation;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_23351 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(st.nextToken());
        int k = stoi.apply(st.nextToken());
        int a = stoi.apply(st.nextToken());
        int b = stoi.apply(st.nextToken());
        int period = n/a;
        if(n % a != 0){
            period++;
        }
        int result = cal(k,b,period);
        System.out.println(result);
    }
    private static int cal(int k, int water, int period){
        if(k < period){
            return k;
        }
        int result = 0;
        while(true){
            k -= period;
            if(k < 0){
                return result + period + k;
            }
            k += water;
            result += period;
        }
    }
}