-
Notifications
You must be signed in to change notification settings - Fork 1
/
P11082.js
38 lines (38 loc) · 1.46 KB
/
P11082.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
const readline = require('readline')
const interface = readline.createInterface(process.stdin)
interface.once('line', line => {
let { groups: { integer, noncyclic, cyclic } } = line.match(/(?<integer>\d+)(?:\.(?<noncyclic>\d*)(?:\((?<cyclic>\d+)\))?)?/)
if (cyclic && [...cyclic].every(c => c === '0')) cyclic = undefined
if (noncyclic && !cyclic && [...noncyclic].every(c => c === '0')) noncyclic = undefined
if (!cyclic) {
if (!noncyclic) {
console.log(`${integer}/1`)
} else {
let numerator = Number(noncyclic)
let divisor = 1;
while (numerator % 2 === 0) {
numerator /= 2;
divisor *= 2;
}
while (numerator % 5 === 0) {
numerator /= 5;
divisor *= 5;
}
const denominator = Number('1' + '0'.repeat(noncyclic.length)) / divisor
console.log(`${integer * denominator + numerator}/${denominator}`)
}
} else {
noncyclic = noncyclic || ''
let numerator = Number(noncyclic + cyclic) - Number(noncyclic)
let denominator = Number('9'.repeat(cyclic.length) + '0'.repeat(noncyclic.length))
let a = numerator, b = denominator
while (a) {
const temp = a
a = b % a
b = temp
}
denominator /= b
numerator /= b
console.log(`${integer * denominator + numerator}/${denominator}`)
}
})