package com.sxt.pratice;
/*
* 百钱买白鸡
* :100文买100只鸡
* 公鸡5文1只,母鸡3文一只,雏鸡1文3只,100文买100只鸡,每个鸡的品种都得有,总共有几种买法
*
* int money = 100;
* int total = 100;
* x+y+z = 100 总数相加为100
* 5x+3y+z/3 = 100; 总钱数为100
* x = 20 公鸡
* y = 33; 母鸡
* z = 300; 小鸡
*
*
*/
public class 白钱买百鸡 {
public static void main(String[] args) {
int num = 100;
int mon = 100;
int count = 0;
//公鸡数量
System.out.println("方法一(低效率)------------------------------------------");
for(int x=0;x<=num;x++){
//母鸡数量
for(int y=0;y<=num;y++){
count++;
//小鸡数量
int z = num-x-y;
if(x>0&&z%3 == 0 && x*5+y*3+z/3 == 100){
System.out.println("公鸡:"+x+" 母鸡:"+y+" 小鸡:"+z);
}
}
}
System.out.println("一共买了"+count+"次");
System.out.println();
System.out.println("方法二(高效率)------------------------------------------");
int count1 = 0;//统计买的次数
//公鸡
for(int x = 1;x<=20;x++){
//母鸡
for(int y= 1;y<=33;y++){
//小鸡
int z = 100-x-y;
count1++;
//同时满足总钱数以及总个数以及小鸡的数目对三取余等于0
if(5*x+3*y+z/3==100 && z%3==0 ){
System.out.println("公鸡:"+x+" 母鸡:"+y+" 小鸡:"+z);
}
}
}
System.out.println(count1);
}
}