题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
const rl = require("readline").createInterface({ input: process.stdin });
let inputArr = []
let outArr = []
rl.on('line', function(line){
inputArr.push(line)
})
rl.on('close', function(){
for(let i=0; i< inputArr.length - 1; i++){
outArr.push (getMax(inputArr[i]))
}
for(let i=0; i< outArr.length; i++){
console.log(outArr[i])
}
})
function getMax(num){
num = parseInt(num)
let a = num % 3 // 剩余空瓶
let b = parseInt(num / 3) // 换到的汽水瓶数
let total = b
let empty = a + b
while(empty > 2){
a = empty % 3 // 剩余空瓶
b = parseInt(empty / 3)
empty = a + b
total = total + b
}
if(empty == 2){
total = total + 1
}
return total
}
