Skip to content

Commit 61556b2

Browse files
committed
[feat] 비트마스크 연습
1 parent 490bde4 commit 61556b2

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

  • src/src/codingTest/Data_Structure/막대기
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package codingTest.Data_Structure.막대기;
2+
3+
import java.util.ArrayList;
4+
import java.util.PriorityQueue;
5+
import java.util.Scanner;
6+
7+
public class Main {
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
int X = sc.nextInt();
11+
12+
PriorityQueue<Integer> pq = new PriorityQueue<>();
13+
pq.add(64);
14+
15+
while (sum(pq) > X) {
16+
Integer shortBar = pq.poll();
17+
int halfBar = shortBar >> 1;
18+
pq.add(halfBar);
19+
if (sum(pq) < X) {
20+
pq.add(halfBar);
21+
}
22+
}
23+
System.out.println(pq.size());
24+
25+
}
26+
27+
public static int sum(PriorityQueue<Integer> pq) {
28+
int sum = 0;
29+
for (Integer integer : pq) {
30+
sum += integer;
31+
}
32+
return sum;
33+
}
34+
}

0 commit comments

Comments
 (0)