鸡兔同笼

《鸡兔同笼》 th.jpg

一个笼子关着若干只鸡和兔子,从上面数共有 35 个头,下面看共有 94 只脚,问笼子里的鸡和兔子各式多少只。

《鸡兔同笼》 analysis.jpg

func main()  {
        x,y := 0,0 //鸡的数量为x,兔的数量为y
        
        head,foot := 35,94//头的数量为head,脚的数量为foot
 
        if foot%2==0 {
            
            /*
            x+y=head
            2*x+4*y=foot
            
            x=2*head-foot/2
             */
            
            x=2*head-foot/2;
            y=head-x;
 
            if x>=0&&y>=0 {
                fmt.Printf("鸡的数量为:%d,兔的数量为:%d",x,y);
            }else{
               fmt.Println("不存在此分配方案!");
            }
 
        }else{
            fmt.Println("不存在此分配方案!");
        }
 

}
fun cal() {
    var x:Int = 0
    var y:Int = 0//

    var head:Int = 35
    var foot:Int = 94//

    if (foot%2==0) {

        /*
        x+y=head
        2*x+4*y=foot

        x=2*head-foot/2
         */

        x=2*head-foot/2;
        y=head-x;

        if (x>=0&&y>=0) {
            println("" + x + "," + y)
        }else{
            println("不存在此分配方案");
        }

    }else{
        println("不存在此分配方案!");
    }

}
(function(){
    var x = 0
    var y = 0//

    var head = 35
    var foot = 94//

    if (foot%2==0) {

        /*
        x+y=head
        2*x+4*y=foot

        x=2*head-foot/2
         */

        x=2*head-foot/2;
        y=head-x;

        if (x>=0&&y>=0) {
            console.log("" + x + "," + y)
        }else{
            console.log("不存在此分配方案");
        }

    }else{
        console.log("不存在此分配方案!");
    }
})()

《鸡兔同笼》 javascript_photo_via_shutterstock.jpg

    原文作者:zidea
    原文地址: https://www.jianshu.com/p/a5f9ba698bb4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞