题解 | #将真分数分解为埃及分数#
将真分数分解为埃及分数
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;
function gongyueshu(a, b) {
// 获取两个数的最大公约数
let yue = 1;
for(let i = a; i>=1; i--) {
if(a%i == 0 && b%i == 0) {
yue = i;
break;
}
}
return yue;
}
void async function () {
// Write your code here
while(line = await readline()){
let tokens = line.split('/');
let a = parseInt(tokens[0]);
let b = parseInt(tokens[1]);
let yueshuMax = gongyueshu(a,b);
let a1 = a/yueshuMax;
let b1 = b/yueshuMax;
// a1:2 b1:9 结果1/9+1/9
// a1:5 b1:9 结果1/9+1/9+1/9+1/9+1/9
let arr = [];
for(let i = 0; i<a1; i++) {
arr.push('1/'+b1);
}
console.log(arr.join('+'));
}
}()
#华为机试题#华为机试题 文章被收录于专栏
华为机试题

