Skip to content

Commit 2e31039

Browse files
author
rainBuildingTree
committed
neg-bin v1.0.
1 parent 5e4de1b commit 2e31039

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

2089_Negative_Binary/a.exe

-162 KB
Binary file not shown.
162 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// HELLO WORLD!
2+
#include <iostream>
3+
#include <stack>
4+
using namespace std;
5+
6+
/*
7+
1. GET input
8+
2. IF input is 0, print 0 and end.
9+
3. WHILE: divide input by -2 until input become 1.
10+
3-1. input is quotient, and keep remainder into a stack.
11+
3-2. if remainder is -1, quotient++ and remainder = -1.
12+
4. PUSH 1 into stack.
13+
5. PRINT from stack.
14+
*/
15+
16+
int main() {
17+
int input;
18+
cin >> input;
19+
20+
if (!input) {
21+
cout << 0;
22+
return 0;
23+
}
24+
25+
stack<int> result;
26+
while (input != 1) {
27+
int quotient = input / (-2);
28+
int remainder = input % (-2);
29+
30+
if (remainder == -1) {
31+
++quotient;
32+
remainder = 1;
33+
}
34+
35+
input = quotient;
36+
result.push(remainder);
37+
}
38+
result.push(1);
39+
40+
while (!result.empty()) {
41+
cout << result.top();
42+
result.pop();
43+
}
44+
45+
return 0;
46+
}

2089_Negative_Binary/pseudo code.txt renamed to Solved Archive/2089_Negative_Binary/pseudo code.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,15 @@
2323
3. print the queue.
2424
*/
2525

26+
/*
27+
1. GET input
28+
2. IF input is 0, print 0 and end.
29+
3. WHILE: divide input by -2 until input become 1.
30+
3-1. input is quotient, and keep remainder into a stack.
31+
3-2. if remainder is -1, quotient++ and remainder = -1.
32+
4. PUSH 1 into stack.
33+
5. PRINT from stack.
34+
*/
35+
36+
2637

0 commit comments

Comments
 (0)