반응형
2089번: -2진수
-2진법은 부호 없는 2진수로 표현이 된다. 2진법에서는 20, 21, 22, 23이 표현 되지만 -2진법에서는 (-2)0 = 1, (-2)1 = -2, (-2)2 = 4, (-2)3 = -8을 표현한다. 10진수로 1부터 표현하자면 1, 110, 111, 100, 101, 11010, 110
www.acmicpc.net
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int decimal = Integer.parseInt(br.readLine());
if (decimal == 0) {
System.out.print(0);
return;
}
while (decimal != 0) {
if (decimal % -2 == 0) {
sb.insert(0, 0);
decimal /= -2;
} else {
sb.insert(0, 1);
decimal = (decimal - 1) / -2;
}
}
System.out.print(sb);
}
}반응형
'Algorithm' 카테고리의 다른 글
| 백준 9095번 1, 2, 3 더하기 [ Java ] (0) | 2021.04.01 |
|---|---|
| 백준 11727번 2×n 타일링 2 [ Java ] (0) | 2021.04.01 |
| 백준 17087번 숨바꼭질 6 [ Java ] (0) | 2021.03.31 |
| 백준 2745번 진법 변환 [ Java ] (0) | 2021.03.31 |
| 백준 11005번 진법 변환 2 [ Java ] (0) | 2021.03.31 |