写前分析:
//大中小三种马共100匹,每种最少一只,大马能带三块转,中马能带两块砖,三匹小马能带一块转,共一百块砖,问大中小马各几只 //三匹马各一只 100 -3 -2- 1/3 剩94又2/3 不考虑第二条件最多加+284只小马 ,每减六只小马能加一只中马,每减九只小马能加一只大马 因此马匹数区间 [39,286],但是增加第二条件(三种马共100匹)一定取不到边界值,所以大致区间在(39,286) //令每三只小马绑定在一块 则有暴力解题法如下
//三重计数循环嵌套暴力解题法 /* //对每种马的区间来三重循环 for(int big_horse = 1;big_horse<33;big_horse++){ for (int mid_horse=1;mid_horse<49;mid_horse++){ for (int small_horse = 1;small_horse<285;small_horse++){ if(small_horse %3 ==0){ //这一步防止small_horse/3向下取整 if(((big_horse*3)+(mid_horse*2)+(small_horse/3)==100) && ( (big_horse+mid_horse+small_horse)==100) ){ System.out.println("大马数量:"+big_horse); System.out.println("中马数量:"+mid_horse); System.out.println("小马数量:"+small_horse); System.out.println("======================================"); } } } } } */
//初步优化解法 //小马/3等于100 - 3*big_horse -2*mid_horse //小马也等于 100 - big_horse -mid_horse 两个等式,三个未知数,解必定不唯一 ==> 300 -9b -6m = 100 -b -m -> 8b + 5m = 200 //八倍的大马数量和五倍的中马数量为200 //预估大马区间[1,32) 中马区间[1,48) for(int big_horse=1;big_horse<32;big_horse++){ for (int mid_horse=1;mid_horse<48;mid_horse++){ if (big_horse*8 + mid_horse*5 ==200){ System.out.println("大马数量:"+big_horse); System.out.println("中马数量:"+mid_horse); System.out.println("小马数量:"+(100 - big_horse -mid_horse)); System.out.println("===================================="); } } }