一 泊松分酒
假设有三个杯子,不同的大小
,三个杯子大小为:12L、8L、5L
。
目的:希望用这三个杯子,来量出一定量的酒
条件:初始酒量: 12L、0L、0L
public class PoissonShareWine {
//三个容器的容量
private int b1 = 12;
private int b2 = 8;
private int b3 = 5;
private int m = 6;//目标值
//各个酒杯的初始状态的酒量
//倒酒的时候,必须要有一定的倒酒策略,否则将会是一场灾难(死循环)
private void pourBottle(int bb1,int bb2,int bb3) {
System.out.println("bb1:"+bb1+";bb2:"+bb2+";bb3:"+bb3);
//b1--->b2--->b3---b1
if(bb1 == m||bb2 ==m||bb3 == m) {
System.out.println("find the bottle");
return;
}
if(bb2!=0&&bb3!=b3) {
if(bb2+bb3<=b3) {
//倒不满
pourBottle(bb1, 0, bb2+bb3);
}else {
pourBottle(bb1, bb2-b3+bb3, b3);
}
}else if(bb3 == b3) {
//酒杯3满了,则往酒杯1倒
if(bb3 + bb1<=b1) {
//说明酒杯1没有满
pourBottle(bb1+bb3, bb2, 0);
}else {
pourBottle(b1, bb2, bb3-(b1-bb1));
}
}else if(bb2 == 0) {
//从酒杯1往酒杯2里倒酒
if(bb1>=b2) {
pourBottle(bb1-b2, b2, bb3);
}else {
pourBottle(0, bb1, bb3);
}
}
}
public static void main(String[] args) {
PoissonShareWine poissonShareWine = new PoissonShareWine();
poissonShareWine.pourBottle(12, 0, 0);
}
}