public class Rabbit_Chicken_Practice {
public static void main(String[] args) {
int legs = 10;
int sum = 3;
//腿必须是偶数
if(legs % 2 == 0) {
//legs / 2必然大于等于sum(只有鸡)并且小于等于2 * sum(只有兔)
if(legs / 2 >= sum && legs / 2 <= 2 * sum) {
//legs:鸡 * 2 = 兔 * 1。所以legs / 2 - sum是兔子的数量。
int rabbitSum = legs / 2 - sum;
int chickenSum = sum - rabbitSum;
System.out.println("rabbit:" + rabbitSum + "chicken:" + chickenSum);
}else {
System.out.println("Out of earth");
}
}else {
System.out.println("Out of earth");
}
}
} |
|