-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulBase.js
More file actions
62 lines (52 loc) · 1.18 KB
/
Copy pathmulBase.js
File metadata and controls
62 lines (52 loc) · 1.18 KB
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
52
53
54
55
56
57
58
59
60
61
62
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek; // 仅返回栈顶元素不删除
this.length = length;
this.clear = clear;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top - 1];
}
function length() {
return this.top;
}
function clear() {
this.top = 0;
}
// // 使用自己实现的Stack
// function mulBase(num, base) {
// var s = new Stack();
// s.push(num % base);
// num = Math.floor(num /= base);
// while (num > 0) {
// s.push(num % base);
// num = Math.floor(num /= base);
// }
// var result = '';
// while (s.length() > 0) {
// result += s.pop();
// }
// return result;
// }
// js原生函数实现
function mulBase(num, base) {
var result = [];
result.push(num % base);
num = Math.floor(num /= base);
while(num > 0) {
result.push(num % base);
num = Math.floor(num /= base);
}
return result.reverse().join('');
}
var newNum = mulBase(32, 2);
print(newNum);