Skip to content

Commit bdb6c66

Browse files
author
rainBuildingTree
committed
base-conv v1.0.
1 parent 00eff0f commit bdb6c66

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
162 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// HELLO WORLD!
2+
#include <iostream>
3+
#include <stack>
4+
using namespace std;
5+
6+
/*
7+
MAIN:
8+
1. GET input and base
9+
2. WHILE: repeat until input becomes 1
10+
2.1. DIV input by base
11+
2.1.1. quotient becomes input
12+
2.1.2. remainder goes to stack
13+
3. PUSH 1 into stack
14+
4. PRINT stack
15+
*/
16+
int main() {
17+
int input;
18+
int base;
19+
cin >> input >> base;
20+
21+
stack<char> output;
22+
while (input >= base) {
23+
char remainder = input % base;
24+
if (remainder > 9)
25+
remainder = 'A' - 10 + remainder;
26+
else
27+
remainder = '0' + remainder;
28+
output.push(remainder);
29+
input = input / base;
30+
}
31+
if (input > 9)
32+
input = 'A' - 10 + input;
33+
else
34+
input = '0' + input;
35+
output.push(input);
36+
37+
while (!output.empty()) {
38+
cout << output.top();
39+
output.pop();
40+
}
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)