大约在一千五百年前,大数学家孙子在《孙子算经》中记载了这样的一道题:“今有雏兔同笼,上有三十五头,下有九十四足,问雏兔各几何?
private static final int head = 35; // 头有35个
private static final int foot = 94; // 脚有94个
private static final int chicken = 2; // 鸡有2只脚
private static final int rabbit = 4; //兔有4只脚
public static void main(String[] args) {
int count_ck,count_rb;
for(count_ck=0;count_ck<foot/2;count_ck++){
for(count_rb=0;count_rb<foot/4;count_rb++){
if((count_rb+count_ck==head) && (count_rb*4+count_ck*2==94)){
System.out.println("鸡:"+count_ck);
System.out.println("兔:"+count_rb);
}
}
}
}
结果:
鸡:23
兔:12