generated from Lidemy/mentor-program-5th
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw5.js
55 lines (50 loc) · 1.03 KB
/
hw5.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
52
53
54
55
const readline = require('readline')
const lines = []
const rl = readline.createInterface({
input: process.stdin
})
rl.on('line', (line) => {
lines.push(line)
})
rl.on('close', () => {
solve(lines)
})
function solve(lines) {
for (let i = 1; i < lines.length; i++) {
const [a, b, k] = lines[i].split(' ')
console.log(compareNumber(a, b, k))
}
}
function compareNumber(a, b, k) {
if (a === b) {
return 'DRAW'
}
// 將比小的規則變成比大的規則須將數字調換
if (k === '-1') {
const temp = a
a = b
b = temp
}
// 比位數則可知道大小
if (a.length > b.length) {
return 'A'
}
if (a.length < b.length) {
return 'B'
}
// 若位數相同則須從字串最左邊的數開始比較
if (a.length === b.length) {
for (let i = 0; i < a.length; i++) {
// 若相等,就繼續
if (a[i] === b[i]) {
continue
}
// 若不相等,就回傳"A""B"
if (a[i] > b[i]) {
return 'A'
} else {
return 'B'
}
}
}
}