Skip to content

Commit ee6f8d7

Browse files
committed
[Silver II] Title: -2진수, Time: 172 ms, Memory: 17700 KB -BaekjoonHub
1 parent 23cc766 commit ee6f8d7

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Silver II] -2진수 - 2089
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2089)
4+
5+
### 성능 요약
6+
7+
메모리: 17700 KB, 시간: 172 ms
8+
9+
### 분류
10+
11+
수학, 정수론
12+
13+
### 제출 일자
14+
15+
2025년 9월 2일 23:03:50
16+
17+
### 문제 설명
18+
19+
<p>-2진법은 부호 없는 2진수로 표현이 된다. 2진법에서는 2<sup>0</sup>, 2<sup>1</sup>, 2<sup>2</sup>, 2<sup>3</sup>이 표현 되지만 -2진법에서는 (-2)<sup>0</sup> = 1, (-2)<sup>1</sup> = -2, (-2)<sup>2</sup> = 4, (-2)<sup>3</sup> = -8을 표현한다. 10진수로 1부터 표현하자면 1, 110, 111, 100, 101, 11010, 11011, 11000, 11001 등이다.</p>
20+
21+
<p>10진법의 수를 입력 받아서 -2진수를 출력하는 프로그램을 작성하시오.</p>
22+
23+
### 입력
24+
25+
<p>첫 줄에 10진법으로 표현된 수 N이 주어진다.</p>
26+
27+
### 출력
28+
29+
<p>-2진법 수를 출력한다.</p>
30+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-13
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
110111
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
3+
class Main {
4+
5+
public static void main(String[] args){
6+
Scanner sc = new Scanner(System.in);
7+
StringBuilder sb = new StringBuilder();
8+
int a = sc.nextInt();
9+
if (a == 0) {
10+
System.out.println(0);
11+
return; // 바로 종료해야 함
12+
}
13+
while (a!=0){
14+
int n = a%(-2);
15+
a/=-2;
16+
if (n<0){
17+
n+=2;
18+
a+=1;
19+
}
20+
sb.append(n);
21+
22+
}
23+
System.out.println(sb.reverse().toString());
24+
}
25+
}

0 commit comments

Comments
 (0)