-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJS.js
51 lines (49 loc) · 1.13 KB
/
JS.js
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
46
47
48
49
50
51
const dic = { T: -1, 1: 1, 0: 0 };
function convertBTToInt(bt) {
bt = bt.split("").reverse().join("");
let value = 0;
for (let i = 0; i < bt.length; i++) {
value += dic[bt[i]] * Math.pow(3, i);
}
return value;
}
function convertIntToBT(n) {
let output = "";
while (n > 0) {
let rem = n % 3;
n = (n - rem) / 3;
if (rem == 2) {
rem = -1;
n++;
}
output = (rem == 0 ? "0" : rem == 1 ? "1" : "T") + output;
}
return output;
}
const lhs = readline();
const op = readline();
const rhs = readline();
let lhsInt = convertBTToInt(lhs);
let rhsInt = convertBTToInt(rhs);
if (op == "+") {
console.log(convertIntToBT(lhsInt + rhsInt));
} else if (op == "-") {
console.log(convertIntToBT(lhsInt - rhsInt));
} else if (op == "*") {
console.log(convertIntToBT(lhsInt * rhsInt));
} else if (op == ">>") {
let str = lhs;
for (let i = 0; i < rhsInt; i++) {
str = str.substring(0, str.length - 1);
if (str.length == 0) {
break;
}
}
console.log(str.length == 0 ? 0 : str);
} else if (op == "<<") {
let str = lhs;
for (let i = 0; i < rhsInt; i++) {
str += "0";
}
console.log(str);
}