

1. 전체 소스
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
'알고리즘(BOJ) > Silver' 카테고리의 다른 글
백준 - 이친수 2193 (다이나믹) (0) | 2023.03.18 |
---|---|
백준 - 1로 만들기 1463 (0) | 2023.03.16 |
백준 - 스네이크버드 16435 (0) | 2023.03.10 |
백준 - A -> B 16953 (0) | 2023.03.09 |
백준 - 신입사원 1946 (0) | 2023.03.08 |