[程序] 用R编程实现下面这个有趣的生活问题

1、问题

某人有100瓶酒,规定4个瓶盖换一瓶,2个空瓶换一瓶,问这个人一共可以得到多少瓶酒?

2、程序

#拥有的酒(以瓶计,不包括瓶盖和瓶身,只代表酒)
liquor <- 0;

#递归函数,输入变量为酒瓶数(包含瓶盖、瓶身)
exchange <- function(wine,bottle,cap){
    liquor <<- liquor + wine;
    tmpBottle <- floor((wine+bottle)*0.5)
    tmpCap <- floor((wine+cap)*0.25)
    cat("tmpBottle:", tmpBottle, ", tmpCap:" , tmpCap, "\n");

    addedLiquor = tmpBottle + tmpCap;
    remainingBottle <- (wine+bottle) - tmpBottle*2;
    remainingCap <- (wine+cap) - tmpCap*4;

    cat("remainingBottle:", remainingBottle, ", remainingCap:" , remainingCap, "\n");

    cat("now own liquor number:",liquor,", addedLiquor number:",addedLiquor,"\n");
        
    if(addedLiquor>=1){
        exchange(addedLiquor,remainingBottle,remainingCap);
    }
}

#赋初始值,并计算
wine <- 100;
exchange(wine,0,0);

#打印结果
cat("total liquor number:" ,liquor,"\n");

3、输出

tmpBottle: 50 , tmpCap: 25
remainingBottle: 0 , remainingCap: 0
now own liquor number: 100 , addedLiquor number: 75
tmpBottle: 37 , tmpCap: 18
remainingBottle: 1 , remainingCap: 3
now own liquor number: 175 , addedLiquor number: 55
tmpBottle: 28 , tmpCap: 14
remainingBottle: 0 , remainingCap: 2
now own liquor number: 230 , addedLiquor number: 42
tmpBottle: 21 , tmpCap: 11
remainingBottle: 0 , remainingCap: 0
now own liquor number: 272 , addedLiquor number: 32
tmpBottle: 16 , tmpCap: 8
remainingBottle: 0 , remainingCap: 0
now own liquor number: 304 , addedLiquor number: 24
tmpBottle: 12 , tmpCap: 6
remainingBottle: 0 , remainingCap: 0
now own liquor number: 328 , addedLiquor number: 18
tmpBottle: 9 , tmpCap: 4
remainingBottle: 0 , remainingCap: 2
now own liquor number: 346 , addedLiquor number: 13
tmpBottle: 6 , tmpCap: 3
remainingBottle: 1 , remainingCap: 3
now own liquor number: 359 , addedLiquor number: 9
tmpBottle: 5 , tmpCap: 3
remainingBottle: 0 , remainingCap: 0
now own liquor number: 368 , addedLiquor number: 8
tmpBottle: 4 , tmpCap: 2
remainingBottle: 0 , remainingCap: 0
now own liquor number: 376 , addedLiquor number: 6
tmpBottle: 3 , tmpCap: 1
remainingBottle: 0 , remainingCap: 2
now own liquor number: 382 , addedLiquor number: 4
tmpBottle: 2 , tmpCap: 1
remainingBottle: 0 , remainingCap: 2
now own liquor number: 386 , addedLiquor number: 3
tmpBottle: 1 , tmpCap: 1
remainingBottle: 1 , remainingCap: 1
now own liquor number: 389 , addedLiquor number: 2
tmpBottle: 1 , tmpCap: 0
remainingBottle: 1 , remainingCap: 3
now own liquor number: 391 , addedLiquor number: 1
tmpBottle: 1 , tmpCap: 1
remainingBottle: 0 , remainingCap: 0
now own liquor number: 392 , addedLiquor number: 2
tmpBottle: 1 , tmpCap: 0
remainingBottle: 0 , remainingCap: 2
now own liquor number: 394 , addedLiquor number: 1
tmpBottle: 0 , tmpCap: 0
remainingBottle: 1 , remainingCap: 3
now own liquor number: 395 , addedLiquor number: 0
total liquor number: 395


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

JavaScript版

(1)程序

      function println (str){
        document.writeln(str +"<br>");
    }

    var wine = 100;
    var liquor = 0;
    
    function exchange(wine,bottle,cap){
         liquor += wine;

        var tmpBottle = Math.floor((wine+bottle)*0.5)
        var tmpCap = Math.floor((wine+cap)*0.25)
        println( "wine:" + wine + ", tmpBottle:" + tmpBottle + ", tmpCap:" + tmpCap + "\n");
    
        var addedLiquor = tmpBottle + tmpCap;
        var remainingBottle = (wine+bottle) - tmpBottle*2;
        var remainingCap = (wine+cap) - tmpCap*4;
        
        println("wine:" + wine +", remainingBottle:" + remainingBottle  +  ", remainingCap:" + remainingCap  +  "\n");

        println("now own liquor number:" + liquor + ", addedLiquor number:" + addedLiquor + "\n");

        
        if(addedLiquor>=1){
            exchange(addedLiquor, remainingBottle,remainingCap);
        }
    }

    exchange(wine,0,0);
    println("total liquor:" + liquor);
(2)输出

 wine:100, tmpBottle:50, tmpCap:25
wine:100, remainingBottle:0, remainingCap:0
now own liquor number:100, addedLiquor number:75
wine:75, tmpBottle:37, tmpCap:18
wine:75, remainingBottle:1, remainingCap:3
now own liquor number:175, addedLiquor number:55
wine:55, tmpBottle:28, tmpCap:14
wine:55, remainingBottle:0, remainingCap:2
now own liquor number:230, addedLiquor number:42
wine:42, tmpBottle:21, tmpCap:11
wine:42, remainingBottle:0, remainingCap:0
now own liquor number:272, addedLiquor number:32
wine:32, tmpBottle:16, tmpCap:8
wine:32, remainingBottle:0, remainingCap:0
now own liquor number:304, addedLiquor number:24
wine:24, tmpBottle:12, tmpCap:6
wine:24, remainingBottle:0, remainingCap:0
now own liquor number:328, addedLiquor number:18
wine:18, tmpBottle:9, tmpCap:4
wine:18, remainingBottle:0, remainingCap:2
now own liquor number:346, addedLiquor number:13
wine:13, tmpBottle:6, tmpCap:3
wine:13, remainingBottle:1, remainingCap:3
now own liquor number:359, addedLiquor number:9
wine:9, tmpBottle:5, tmpCap:3
wine:9, remainingBottle:0, remainingCap:0
now own liquor number:368, addedLiquor number:8
wine:8, tmpBottle:4, tmpCap:2
wine:8, remainingBottle:0, remainingCap:0
now own liquor number:376, addedLiquor number:6
wine:6, tmpBottle:3, tmpCap:1
wine:6, remainingBottle:0, remainingCap:2
now own liquor number:382, addedLiquor number:4
wine:4, tmpBottle:2, tmpCap:1
wine:4, remainingBottle:0, remainingCap:2
now own liquor number:386, addedLiquor number:3
wine:3, tmpBottle:1, tmpCap:1
wine:3, remainingBottle:1, remainingCap:1
now own liquor number:389, addedLiquor number:2
wine:2, tmpBottle:1, tmpCap:0
wine:2, remainingBottle:1, remainingCap:3
now own liquor number:391, addedLiquor number:1
wine:1, tmpBottle:1, tmpCap:1
wine:1, remainingBottle:0, remainingCap:0
now own liquor number:392, addedLiquor number:2
wine:2, tmpBottle:1, tmpCap:0
wine:2, remainingBottle:0, remainingCap:2
now own liquor number:394, addedLiquor number:1
wine:1, tmpBottle:0, tmpCap:0
wine:1, remainingBottle:1, remainingCap:3
now own liquor number:395, addedLiquor number:0
total liquor:395

---------------------------------------------------------------------------------------------------------------------------------------------------------------

数学方法(考虑借)

如果用数学方法,这个问题将变得很简单:
假设瓶身、瓶盖、酒的价值分别为x, y, z,4个瓶盖或2个瓶身可以换一瓶酒,那么有:
2x=4y => x=2y

根据2个瓶身(或4个瓶盖)换1瓶酒(包含酒瓶),有:
2x = x + y + z
整理为:
x = y + z

联立方程:
x=2y
x=y+z

从而有:
y = z        (1)
x = 2z      (2)
即1个瓶身就可以换1瓶酒(不包含瓶盖、瓶身,仅仅指瓶内的酒),同理,2个瓶盖可以换1瓶酒。

现在总资产:
t = 100(x+y+z)      (3)
将(1)(2)式带入式(3),则有:
t= 100(2z + z +z) = 400z

也就是说,总共价值可折合成400瓶酒(仅仅指瓶中的酒)。

======================================================================================================

如果用上面的原理解释前述计算最后剩的1个空瓶和3个瓶盖可以换几瓶酒,则:
1个空瓶可以换2瓶酒(仅指酒),1个瓶盖可以换1瓶酒,
因此,总共还可以换5瓶酒(2+3*1)。所以若考虑可以借酒瓶和瓶盖,总共可以换395+5=400瓶酒。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值