-
[백준] 1094번 : 막대기 [JAVA]Algorithm/백준[JAVA] 2024. 5. 8. 22:58
https://www.acmicpc.net/problem/1094
# 문제
# 접근방식
- 지민이가 가지고 있는 막대기가 x와 같을 때까지 2로 나누어주는 방식이다.
- 반으로 나눈 값들을 합한 것이 x와 같으면 출력해주면 된다.
- while문을 사용하여 x > 0일때까지 반복해준다.
- 지민이가 가지고 있는 막대기(stick)가 x보다 크면 stick/2를해준다.
- stick이 x와 같거나 작다면 count++해준다.
- count했기 때문에 x에서 stick값만큼 빼준다.
# 소스코드
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int x = Integer.parseInt(br.readLine()); int stick = 64; int count = 0; while(x > 0){ if(stick > x){ stick /= 2; } else { count++; x -= stick; } } System.out.println(count); } }
# 성능
#회고
비트마스킹
'Algorithm > 백준[JAVA]' 카테고리의 다른 글
[백준] 1074번 : Z [JAVA] (0) 2024.05.10 [백준] 1012번 : 유기농 배추[JAVA] (0) 2024.05.09 [백준] 1032번 : 명령 프롬프트 [JAVA] (0) 2024.05.06 [백준] 1267번 : 핸드폰 요금 [JAVA] (0) 2024.05.05 [백준] 1247번 : 부호 [JAVA] (0) 2024.05.05