Java倒油的问题

发表于:2010-7-26 11:13

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

#
java

  用JAVA解答~ 有一位厨师要从12斤油(A桶)倒出6斤油但现在手里只有8斤和5斤的桶,怎么取出6斤

import java.util.ArrayList;
public class DaoYou {
public static void main(String[] args) {
  // TODO Auto-generated method stub
  ArrayList<State> stateList = new ArrayList<State>();
  Tong t1 = new Tong("5斤桶",5,0);
  Tong t2 = new Tong("8斤桶",8,0);
  Tong t3 = new Tong("12斤桶",12,12);
  DaoYou dy = new DaoYou();
  State st = new State(t1,t2,t3,0,0);
  stateList.add(st);
  try {
  dy.dao(st, stateList);
  } catch (CloneNotSupportedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }

  for(int i=0; i< stateList.size(); i++){
    System.out.println(stateList.get(i));
  }
}

public boolean dao(State s,ArrayList<State> states) throws CloneNotSupportedException{
Tong tempT1 = (Tong)s.getT1().clone();
Tong tempT2 = (Tong)s.getT2().clone();
Tong tempT3 = (Tong)s.getT3().clone();
//t1 向 t2 里边倒油
//还可以对倒油的方法单独拿出来,减少代码的冗余
  if(tempT1.getNow() > 0 && tempT2.getNow()<tempT2.getMax()){
    Tong t1 = (Tong)tempT1.clone();
    Tong t2 = (Tong)tempT2.clone();
    Tong t3 = (Tong)tempT3.clone();
    int temp = (tempT1.getNow() > tempT2.getMax() - tempT2.getNow()) ?
    tempT2.getMax() - tempT2.getNow() : tempT1.getNow();
    t1.setNow(t1.getNow() - temp);
    t2.setNow(t2.getNow() + temp);
    State.NEWID = State.NEWID + 1;
    State st = new State(t1,t2,t3,State.NEWID,s.getId());
      if (states.contains(st)){
      State.NEWID = State.NEWID - 1;
      State tempState = states.get(states.indexOf(st));
        if (s.getId() < tempState.getId()){
        System.out.println("======向前的捷径倒油方法======"+ states.indexOf(st) + "\t" + s + "\t"   + states.get(states.indexOf(st)));
        }else {
        System.out.println("============"+ states.indexOf(st) + "\t" + s + "\t"   + states.get(states.indexOf(st)));
        }
      }else {
      states.add(st);
      //     如果倒油成功,返回
      //     if(t1.getNow() == 6 || t2.getNow() == 6 || t3.getNow() == 6){
      //      return true;
      //     }
      dao(st,states);
      }
    }

  //t2向t1里倒油
    if(tempT2.getNow() > 0 && tempT1.getNow()<tempT1.getMax()){
    Tong t1 = (Tong)tempT1.clone();
    Tong t2 = (Tong)tempT2.clone();
    Tong t3 = (Tong)tempT3.clone();
    int temp = (tempT2.getNow() > tempT1.getMax() - tempT1.getNow()) ?
    tempT1.getMax() - tempT1.getNow() : tempT2.getNow();
    t2.setNow(t2.getNow() - temp);
    t1.setNow(t1.getNow() + temp);
    State.NEWID = State.NEWID + 1;
    State st = new State(t1,t2,t3,State.NEWID,s.getId());
      if (states.contains(st)){
      State.NEWID = State.NEWID - 1;
        State tempState = states.get(states.indexOf(st));
        if (s.getId() < tempState.getId()){
        System.out.println("======向前的捷径倒油方法======"+ states.indexOf(st) + "\t" + s + "\t"   + states.get(states.indexOf(st)));
        }else {
        System.out.println("============"+ states.indexOf(st) + "\t" + s + "\t"   + states.get(states.indexOf(st)));
        }
      }else {
      states.add(st);
      //     如果倒油成功,返回
      //     if(t1.getNow() == 6 || t2.getNow() == 6 || t3.getNow() == 6){
      //      return true;
      //     }
      dao(st,states);
      }
    }

51/512345>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号