鸡兔同笼

作者: zidea | 来源:发表于2019-04-01 20:21 被阅读10次
    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

    相关文章

      网友评论

        本文标题:鸡兔同笼

        本文链接:https://www.haomeiwen.com/subject/iqtmbqtx.html