알고리즘(BOJ)/Silver
백준 - 물병 1052
ch.0
2023. 3. 14. 21:42
전체 소스
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int rst = buyBottle(N, K);
System.out.println(rst);
br.close();
}
private static int buyBottle(int n, int k) {
if(n <= k) return 0;
int iterations = k - 1;
int pow2 = 1;
while (pow2 < n) {
pow2 *= 2;
}
while (iterations > 0) {
pow2 /= 2;
n -= pow2;
if(n == 0) return 0;
iterations--;
}
pow2 /= 2;
return pow2 - n;
}
}
https://www.acmicpc.net/problem/1052
1052번: 물병
지민이는 N개의 물병을 가지고 있다. 각 물병에는 물을 무한대로 부을 수 있다. 처음에 모든 물병에는 물이 1리터씩 들어있다. 지민이는 이 물병을 또 다른 장소로 옮기려고 한다. 지민이는 한 번
www.acmicpc.net