一个笼子关着若干只鸡和兔子,从上面数共有 35 个头,下面看共有 94 只脚,问笼子里的鸡和兔子各式多少只。
analysis.jpgfunc 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
网友评论