51,411
社区成员
发帖
与我相关
我的任务
分享
public class SanTongShui {
private Tong daTong;
private Tong zhongTong;
private Tong xiaoTong;
private int count; // 倒水次数
public SanTongShui() {
daTong = new Tong("大", 30, 30);
zhongTong = new Tong("中", 17, 0);
xiaoTong = new Tong("小", 13, 0);
statusHead();
}
public void oper() {
if (isStop()) {
System.out.println("倒水" + count + "次,结束");
return;
}
if (xiaoTong.isFull()) {
daoshui(xiaoTong, daTong);
} else if (zhongTong.isEmpty()) {
daoshui(daTong, zhongTong);
} else {
daoshui(zhongTong, xiaoTong);
}
showStatus();
oper();
}
private void statusHead() {
System.out.println("\t" + daTong.getName() + "\t" + zhongTong.getName()
+ "\t" + xiaoTong.getName());
}
private void showStatus() {
System.out.println(daTong.getH() + "\t" + zhongTong.getH() + "\t"
+ xiaoTong.getH());
}
public boolean isStop() {
if (daTong.getH() == zhongTong.getH() && zhongTong.getH() == 15)
return true;
return false;
}
/**
* t1 - > t2,t1 水倒入 t2 中,倒入后有两种情况,t2 水满或 t1 水空。
*
* @param t1
* @param t2
*/
public void daoshui(Tong t1, Tong t2) {
int n; // t1 倒出的水量
if (t1.getH() >= t2.freeSize())
n = t2.freeSize();
else
n = t1.getH();
t1.setH(t1.getH() - n);
t2.setH(t2.getH() + n);
count++;
System.out.print(t1.getName() + "->" + t2.getName() + "\t");
}
class Tong {
int max; // 容积
int h; // 当前水量
String name;
public Tong(String name, int cubage, int h) {
if (h > cubage)
throw new RuntimeException("当前水量不能大于水桶容积");
this.name = name;
this.max = cubage;
this.h = h;
}
/**
* 可用体积
*
* @return
*/
public int freeSize() {
return max - h;
}
public boolean isFull() {
return max == h;
}
public boolean isEmpty() {
return h == 0;
}
public String getName() {
return name;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
}
/**
* @param args
*/
public static void main(String[] args) {
new SanTongShui().oper();
}
}/*output
大 中 小
大->中 13 17 0
中->小 13 4 13
小->大 26 4 0
中->小 26 0 4
大->中 9 17 4
中->小 9 8 13
小->大 22 8 0
中->小 22 0 8
大->中 5 17 8
中->小 5 12 13
小->大 18 12 0
中->小 18 0 12
大->中 1 17 12
中->小 1 16 13
小->大 14 16 0
中->小 14 3 13
小->大 27 3 0
中->小 27 0 3
大->中 10 17 3
中->小 10 7 13
小->大 23 7 0
中->小 23 0 7
大->中 6 17 7
中->小 6 11 13
小->大 19 11 0
中->小 19 0 11
大->中 2 17 11
中->小 2 15 13
小->大 15 15 0
倒水29次,结束
*/
class A//三桶水问题 30 17 13 得到两个15//定义一个桶子类
class Barrel{
private Integer capacity; //容量
private Integer current;//当前水量
public void setCapacity(Integer capacity){
this.capacity=capacity;
}
public Integer getCapacity(){
return this.capacity;
}
public void setCurrent(Integer current){
this.current=current;
}
public Integer getCurrent(){
return this.current;
}
}
public class TestString {
//把水从barrel1倒入barrel2
public static void EnterWater(Barrel barrel1,Barrel barrel2){
if(barrel1.getCurrent()!=0&&barrel2.getCurrent()<barrel2.getCapacity()){//如果barrel1有水并且barrel2没有装满
Integer temp=0;
Integer current1=barrel1.getCurrent();
Integer current2=barrel2.getCurrent();
if(current1>(barrel2.getCapacity()-current2)){//如果barrel1里面的水可以把barrel2装满
temp=barrel2.getCapacity()-current2;
barrel1.setCurrent(current1-temp);
barrel2.setCurrent(barrel2.getCapacity());
}
else{//否则
barrel2.setCurrent(current1+current2);
barrel1.setCurrent(0);
}
}
}
public static void main(String[] args) {
Barrel A=new Barrel();
Barrel B=new Barrel();
Barrel C=new Barrel();
A.setCapacity(30);
B.setCapacity(17);
C.setCapacity(13);
//开始进行
Integer time=0;//计算次数的
A.setCurrent(30);
B.setCurrent(0);
C.setCurrent(0);
/*A.getCurrent()!=15&&B.getCurrent()!=15*/
while(true){
EnterWater(A,B);time++;
System.out.println(A.getCurrent()+" "+B.getCurrent()+" "+C.getCurrent());
EnterWater(B,C);time++;
System.out.println(A.getCurrent()+" "+B.getCurrent()+" "+C.getCurrent());
EnterWater(C,A);time++;
System.out.println(A.getCurrent()+" "+B.getCurrent()+" "+C.getCurrent());
if(A.getCurrent()==15&&B.getCurrent()==15){
break;
}
EnterWater(B,C);time++;
System.out.println(A.getCurrent()+" "+B.getCurrent()+" "+C.getCurrent());
if(C.getCurrent()==C.getCapacity()){//此时如果C满了
EnterWater(C,A);time++;
System.out.println(A.getCurrent()+" "+B.getCurrent()+" "+C.getCurrent());
EnterWater(B,C);time++;
System.out.println(A.getCurrent()+" "+B.getCurrent()+" "+C.getCurrent());
}
}
System.out.println("一共进行了"+time+"操作");
}
}
