Skip to content

Commit 3fdefe4

Browse files
committed
1052 - 물병 (비트마스킹, 그리디)
1 parent ebfd461 commit 3fdefe4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
- <a href="https://www.acmicpc.net/problem/2258">2258. 정육점</a>
184184
- <a href="https://www.acmicpc.net/problem/2513">2513. 통학버스</a>
185185
- <a href="https://www.acmicpc.net/problem/18768">18768. 팀 배정</a>
186+
- <a href="https://www.acmicpc.net/problem/1052">1052. 물병</a>
186187
</pre>
187188
</div>
188189
</details>

src/greedy/n1052/Main.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package greedy.n1052;
2+
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
Scanner sc = new Scanner(System.in);
8+
int N = sc.nextInt();
9+
int K = sc.nextInt();
10+
11+
int ans = 0;
12+
13+
// Integer.bitCount(정수): 주어진 정수에서 true의 bit 개수를 찾는 함수
14+
while (Integer.bitCount(N) > K) {
15+
ans += N & (-N);
16+
N += N & (-N);
17+
}
18+
19+
System.out.println(ans);
20+
}
21+
}

0 commit comments

Comments
 (0)