84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝几瓶,要用js实现
ringa_lee
function computed(money) { var num = parseFloat(money) / 2; var pingzi = num, gaizi = num; var total = num; reComputed(); function reComputed() { if (pingzi < 2 && gaizi < 4) { return; } if (pingzi >= 2) { var beishu = Math.floor(pingzi / 2); pingzi = pingzi - beishu * 2 + Math.floor(pingzi / 2); gaizi = gaizi + beishu; total = total + beishu; } else { var beishu_1 = Math.floor(gaizi / 4); gaizi = gaizi - beishu_1 * 4 + Math.floor(gaizi / 4); pingzi = pingzi + beishu_1; total = total + beishu_1; } reComputed(); } return total; }
输入金钱10,输出15,所有10元最多应该可以喝15瓶
有些东西用计算机要简单一些,但不是所有的计算用计算机都要简单一点!
测试 test 啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝几瓶,要用js实现.下面输入所花费用 计算
啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝几瓶,要用js实现.下面输入所花费用
function howMany(price, buttle, cap){/*兑换所需单价 瓶子 瓶盖*/ return function get(money, sum = 0, b = 0, pg = 0){ var n = money ? (money / price) << 1 >> 1 : ((b / buttle) << 1 >> 1) + ((pg / cap) << 1 >> 1); return !n ? sum : get(0, sum + n, b % buttle + n, pg % cap + n); } } var count = howMany(2, 2, 4); count(10)
怀念pg作为某种货币的时光
def beer(total, cover, bottle):
if cover < 4 and bottle < 2: return total if cover >= 4: total, bottle, cover = total+cover/4, bottle+cover/4, cover%4+cover/4 if bottle >= 2: total, cover, bottle = total+bottle/2, cover+bottle/2, bottle%2+bottle/2 return beer(total, cover, bottle)
print beer(10/2, 10/2, 10/2)
感觉就得用递归啊
var fn=function(x,y,z,m){//x是完整瓶,y是空瓶,z是空盖,m是能喝的瓶数 return x==0&&y<2&&z<4?m:fn(parseInt(y/2)+parseInt(z/4),y%2+x,z%4+x,m+=x)
}var num=fn(10/2,0,0,0)
输入金钱10,输出15,所有10元最多应该可以喝15瓶
有些东西用计算机要简单一些,但不是所有的计算用计算机都要简单一点!
怀念pg作为某种货币的时光
def beer(total, cover, bottle):
print beer(10/2, 10/2, 10/2)
感觉就得用递归啊
}
var num=fn(10/2,0,0,0)