File tree Expand file tree Collapse file tree 5 files changed +57
-0
lines changed
Solved Archive/2089_Negative_Binary Expand file tree Collapse file tree 5 files changed +57
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 23
23
3. print the queue.
24
24
*/
25
25
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
+
26
37
You can’t perform that action at this time.
0 commit comments