javascript - 啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝几瓶,要用js实现
ringa_lee
ringa_lee 2017-04-11 10:36:48
0
6
2920

啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝几瓶,要用js实现

ringa_lee
ringa_lee

ringa_lee

reply all (6)
巴扎黑
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瓶

    小葫芦

    有些东西用计算机要简单一些,但不是所有的计算用计算机都要简单一点!

      伊谢尔伦
         测试  
      

      啤酒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作为某种货币的时光

          PHPzhong

          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)

              Latest Downloads
              More>
              Web Effects
              Website Source Code
              Website Materials
              Front End Template
              About us Disclaimer Sitemap
              php.cn:Public welfare online PHP training,Help PHP learners grow quickly!