-
Notifications
You must be signed in to change notification settings - Fork 0
/
2089.cpp
45 lines (40 loc) · 932 Bytes
/
2089.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
using namespace std;
void Solution2089() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int decimal_number; // 입력받은 10진수
cin >> decimal_number;
// 입력받은 수를 -2로 계속 나눠서 -2진수로 만들음
string result = "";
char temp;
while (!(decimal_number == 0 || decimal_number == 1)) {
if (decimal_number < 0) {
temp = '0' + (decimal_number % 2 * (-1));
result.push_back(temp);
decimal_number = decimal_number / (-2);
if (temp == '1') {
decimal_number++;
}
}
else {
temp = '0' + (decimal_number % 2);
result.push_back(temp);
decimal_number = decimal_number / (-2);
}
}
if (decimal_number == 1) {
result.push_back('1');
}
else if (decimal_number == 0) {
result.push_back('0');
}
for (int i = result.size() - 1; i >= 0; i--)
{
cout << result.at(i);
}
}
int main(void) {
Solution2089();
return 0;
}