题解 | #汽水瓶#
汽水瓶
https://www.nowcoder.com/practice/fe298c55694f4ed39e256170ff2c205f
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
while(line = await readline()){
if(line == '0') return;
let emptySize = Number(line);
// 统计喝了多少
let total = 0;
// 计算喝了多少
while(emptySize > 2) {
// 喝多少
const he = Math.floor(emptySize/3);
// console.log(he)
total += he;
// 剩余多少 = 总个数 - 换掉的个数 + 剩余的个数
emptySize = emptySize - 3 * he + he;
}
// 如果剩余的有二个可以再换一个
if(emptySize == 2) {
total++;
}
console.log(total)
}
}()
当看到别人写的优秀算法,感觉自己的太LOW了

查看17道真题和解析