题目:
1只鸡有1个头,2只脚,1只兔子有1个头,4只脚,若已知头的数量和脚的数量,求鸡和兔子各有多少?
import java.util.Scanner;
public class JTTL {
public static void main(String[] args) {
int x,y;//鸡的数量为x,兔的数量为y
int head,foot;//头的数量为head,脚的数量为foot
Scanner sc=new Scanner(System.in);
System.out.print("请输入头的数量:");
head=sc.nextInt();
System.out.print("请输入脚的数量:");
foot=sc.nextInt();
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){
System.out.println("鸡的数量为:"+x+",兔的数量为:"+y);
}else{
System.out.println("不存在此分配方案!");
}
}else{
System.out.println("不存在此分配方案!");
}
sc.close();
}
}