A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

有三个杯子
一个8升一个5升一个三升
只有8升的杯子里装满了水
问怎么用这3个杯子
得到两杯4升的水

10 个回复

正序浏览
代码这么长
回复 使用道具 举报
使用递归!
  1. import java.util.*;
  2. class Test_1
  3. {
  4.     // 三个杯子
  5.     private static int CupA = 8;
  6.     private static int CupB = 5;
  7.     private static int CupC = 3;

  8.     // 保存倒水的状态,不能又重复的,避免死循环
  9.     private static ArrayList<String> history = new ArrayList<String>();

  10.     public static void main(String[] args)
  11.     {
  12.         putWater(8,0,0,"8-0-0");
  13.     }

  14.     public static void putWater(int a, int b, int c, String str)
  15.     {
  16.         // 存在了这种状态了,不能继续,否则死循环
  17.         if(history.contains(str))
  18.             return;

  19.         //System.out.println(str);
  20.         if(a == 4 && b == 4)
  21.         {
  22.             history.add(str);
  23.             System.out.println(history);
  24.             System.out.println("OK");
  25.             return;
  26.         }
  27.                     
  28.         history.add(str);

  29.         // 优先填补0容量的杯子,A给B
  30.         if(a != 0 && b == 0)
  31.         {
  32.             int tempa = a + b > CupB ? a + b - CupB : 0;
  33.             int tempb = a + b > CupB ? CupB : a + b;
  34.             putWater(tempa, tempb, c , tempa + "-" + tempb + "-" + c);
  35.         }
  36.         else
  37.         {
  38.             // B给C
  39.             if(b != 0 && c == 0)
  40.             {
  41.                 int tempb = b + c > CupC ? b + c - CupC : 0;
  42.                 int tempc = b + c > CupC ? CupC : b + c;
  43.                 putWater(a, tempb, tempc, a + "-" + tempb + "-" + tempc);
  44.             }
  45.             // A杯倒给B杯
  46.             else if(a == CupA)
  47.             {
  48.                 int tempa = a + b > CupB ? a + b - CupB : 0;
  49.                 int tempb = a + b > CupB ? CupB : a + b;
  50.                 putWater(tempa, tempb, c , tempa + "-" + tempb + "-" + c);
  51.             }
  52.             // B杯倒给C杯
  53.             else if(b == CupB)
  54.             {
  55.                 int tempb = b + c > CupC ? b + c - CupC : 0;
  56.                 int tempc = b + c > CupC ? CupC : b + c;
  57.                 putWater(a, tempb, tempc, a + "-" + tempb + "-" + tempc);
  58.             }
  59.             // C杯倒给A杯
  60.             else if(c == CupC)
  61.             {
  62.                 int tempc = c + a > CupA ? c + a - CupA : 0;
  63.                 int tempa = c + a > CupA ? CupA : c + a;
  64.                 putWater(tempa, b, tempc, tempa + "-" + b + "-" + tempc);
  65.             }
  66.         }
  67.     }
  68. }
复制代码



回复 使用道具 举报
那个八升的杯子不要动,把那个5升杯子的水,拿一升倒到那个三升的杯子不就行了
回复 使用道具 举报
我觉的是不是用数组做出来的啊
回复 使用道具 举报
代码要怎么写啊  难道是for嵌套?
回复 使用道具 举报
楼上的楼上说的有道理。
回复 使用道具 举报
这不是个数学题么。。。。
回复 使用道具 举报
楼上的好高端
回复 使用道具 举报
本帖最后由 JYcainiao 于 2015-10-13 23:10 编辑

8 5 3 容量
5 0 3 水
2 3 3  水
2 5 1 水
7 0 1 水
7 1 0 水
4 1 3 水
4 4 0 水
回复 使用道具 举报 2 0
怎么得到的好说  关键是思路是什么、
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马