[백준]_1052_물병

`.

물병을 2의 배수만큼 만들 수 있어서 이진법으로 쪽으로 생각하다가 2를 나누면서 나오는 1의 개수가 물병 개수라고 생각할 수 있었음.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B1052 {
	static int N, M,cnt=0;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		
		while(true) {
			int k = N + cnt, one = 0;
			// 나머지가 1일 경우 물병 하나 생성
			while(k!=0) {
				one+=k%2;
				k/=2;
			}
			// 만들어지는 물병 수가 M이하
			if(one <= M) {
				break;
			}else cnt++;
		}
		System.out.println(cnt);
	}

}
updatedupdated2021-02-262021-02-26
Load Comments?