稚兔同笼(鸡兔同笼)问题,JAVA代码实现并不难,主要是数学算法,小学书上面一般都写了的,或者百度一下也行:
我参考鸡兔同笼问题不同阶段的不同解法写了四种方法,并写了个java8 Stream流版本:
/*** 笼子,关有鸡和兔子*/publicstaticclassCage{/*** 鸡*/privateIntegerchicken=0;/*** 兔*/privateIntegerrabbit=0;publicIntegergetChicken(){returnchicken;}publicvoidsetChicken(Integerchicken){this.chicken=chicken;}publicIntegergetRabbit(){returnrabbit;}publicvoidsetRabbit(Integerrabbit){this.rabbit=rabbit;}@OverridepublicStringtoString(){return"稚兔同笼{"+"稚="+chicken+", 兔="+rabbit+'}';}}/*** 笼子中已知头的总数*/privatestaticfinalintHEAD=35;/*** 笼子中已知脚的总数*/privatestaticfinalintFOOT=94;/*** 稚兔同笼*/publicstaticvoidmain(String[]args){System.out.println("穷举简易方程式解法:"+exhaustive());System.out.println("化用简易方程式求解法:"+simpleEquation());System.out.println("假设法:"+hypothesis());System.out.println("砍脚法:"+choppingFeet());System.out.println("穷举简易方程式解法 java8 Stream版:");exhaustiveStream();}/*** 穷举简易方程式解法*/privatestaticCageexhaustive(){for(intchicken=0;chicken<HEAD;chicken++){if(2*chicken+4*(HEAD-chicken)==FOOT){intrabbit=