某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。
小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。
数据范围:输入的正整数满足
注意:本题存在多组输入。输入的 0 表示输入结束,并不用输出结果。
输入文件最多包含 10 组测试数据,每个数据占一行,仅包含一个正整数 n( 1<=n<=100 ),表示小张手上的空汽水瓶数。n=0 表示输入结束,你的程序不应当处理这一行。
对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。
3 10 81 0
1 5 40
样例 1 解释:用三个空瓶换一瓶汽水,剩一个空瓶无法继续交换 样例 2 解释:用九个空瓶换三瓶汽水,剩四个空瓶再用三个空瓶换一瓶汽水,剩两个空瓶,向老板借一个空瓶再用三个空瓶换一瓶汽水喝完得一个空瓶还给老板
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()){ if (line == '0') continue; console.log(Number(line) >> 1); } }()
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const lines = []; rl.on("line", function (line) { if (line === "0") { return; } const f = parseInt(line); let count = 0; let drink = Math.floor(f / 3); // 换 let rest = f % 3; // 余下几瓶 count = count + drink; while (drink > 0) { const temp1 = Math.floor((drink + rest) / 3); const rest1 = (drink + rest) % 3; count = count + temp1; drink = temp1; rest = rest1; // 加入借的场景 if (temp1 === 0 && rest1 === 2) { count++; } } console.log(count); });
SB解法
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let list = []; rl.on("line", function (line) { list.push(line); }); rl.on("close", function (line) { for (let i = 0; i < list.length; i++) { let num = 0; let d = parseInt(list[i]); if (d === 0) { break; } if (d < 2) { console.log(0); } else { num = fn(d, num); } console.log(num); } }); function fn(d, num) { let y = d % 3; let s = parseInt(d / 3); num = num + s; if (s + y > 2) { let newst = y + s; num = fn(newst, num); return num; } else if (s + y == 2) { num = num + 1; return num; } else { return num; } }
//方法1: 直接用数学方法分析,别问,我也是看题解才想明白的 while(line = parseInt(readline())){ console.log(Math.floor(line / 2)) } //方法2: 递归 var arr = []; //这个判断会自动帮我们把最后用于结束的 0 给省略 while(line = parseInt(readline())){ arr.push(line); } //工具函数 var recursion = (n) => { let res = 0; //拥有的空瓶大于3,就可以进行操作 while(n >= 3){ //先正常换,每次把换到的汽水记录到res res += Math.floor(n / 3); //每次换完之后,重新维护空瓶数n,进行下一轮换汽水 n = Math.floor(n / 3) + n % 3; } //空汽水瓶为2时,才需要借一个,少了(n == 1)还不起,多了(n >= 3)没必要借 if(n == 2) n++; return res; } //依次输出答案 for(let n of arr){ console.log(recursion(n)) }
let n = parseInt(readline());
let sum = 0;
function fn1(n) {
let a = Math.floor(n / 3);//每次能换的瓶数
sum += a;//换的瓶数总和
n = a + n % 3;//更新空瓶数
if (n > 2) {//若空瓶数大于2 函数继续执行
return fn1(n);
} else if (n === 2) {//若空瓶数等于2 借老板一瓶,即又换了一瓶(sum+1),最后三空瓶还给老板,函数结束
return sum += 1
} else {//若空瓶小于2,则返回sum,函数结束
return sum
}
}
console.log(fn1(n));
var line; while(line = readline()){ var lines = line.split('\n'); var n = parseInt(lines[0]); function change(n){ var exnum = 3; var soda = 0; var back = 0; if(n == 0){ soda = 0; } while(n >= exnum){ back = Math.floor(n/exnum); n = n%exnum+back; soda+=back; } if(n == exnum-1){ var borrow = 1; soda = soda+borrow; } return soda; } print(change(n)); }
let indata =[]; while(line = readline()) { if(line == 0) {break;} indata.push(line); } var foo=function(){ let out=[]; for(let i of indata){ let s=i,t=0; while( s >= 3){ t = t + Math.floor( s/3); s = s%3 + Math.floor( s/3); } if(s == 2) {t+=1}; print(t); } } foo()
while(line = readline()){ console.log(Math.ceil((Number(line)-1)/2)) }
var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.on('line', function (input) { if(input !== '0'){ let left = Number(input) let result = 0 while(left >= 3){ const newBottles = Math.floor(left/3) result += newBottles left = left % 3 + newBottles } if(left === 2) result++ console.log(result) } });
let line; while(line = readline()) { print (Math.floor(line / 2)); }顺便问一下究竟js该怎么处理牛客的acm模式的stdio,readline方法没太折腾明白,人已经傻了
var readline = require('readline') const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) var count = 0 rl.on('line', (line) => { line = parseInt(line) if(line === 0) return while(line > 1) { var temp = parseInt(line/3) count += temp line = temp + line % 3 if(line === 2){ line += 1 } } console.log(count) })问什么这样写,输出结果不对,在浏览器下测试没有问题啊
const getRes = function(n,z){ let s = Math.floor(n/3)//此次能换的瓶子 z = z + s //res let y = n%3 + s//剩余的瓶子 if(y > 3){ return getRes(y,z) }else if(y >=2){ return z+1 }else{ return z } } let n while(n=readline()){ if(n===0){ break }//如果输入0 终止程序 n=Number(n) console.log(getRes(n,0)) }