题解 | #将真分数分解为埃及分数#
将真分数分解为埃及分数
https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b
题解如下:
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while(line = await readline()){
let arr = line.split('/').map(Number)
let max = getMax(arr[0], arr[1])
arr = arr.map(n => n/max)
if(arr[0]==1){
console.log('1/'+arr[1])
}else{
let temp = []
while((arr[0]!==1) && (arr[1]%(arr[0]-1)!==0)){
let chu = parseInt(arr[1]/arr[0])+1
temp.push(chu)
arr[0] = arr[0]*chu - arr[1]
arr[1] = arr[1]*chu
let max = getMax(arr[0], arr[1])
arr = arr.map(n => n/max)
}
if(arr[0] != 1) temp.push(arr[1]/(arr[0]-1))
temp.push(arr[1])
temp = temp.map(n => '1/'+n)
console.log(temp.join('+'))
}
}
function getMax(a,b){
if(a%b==0) return b
return getMax(b,a%b)
}
}()