题目:
有三口酒缸,分别能装3斤;7斤;10斤。现在10斤的缸装满了酒,在没有称得情况下,怎么把这10斤酒平均分成两个5斤。
解答:
第一步,用10斤的先倒入3斤的,将3斤的装满,将3斤的倒入7斤的,再将10斤的缸子中的7斤倒入3斤的装满,将3斤的再倒入7斤的,最后再将10中剩下的4斤倒入3斤的缸子,此时,三个缸子的状态为,10斤中有1斤,7斤的缸子中有6斤,3斤的缸子中有3斤。
第二步,用3斤的将7斤的装满,状态为:10斤中有1斤,7斤中有7斤,3斤中有2斤。
第三步,将7斤的缸子里的酒全部倒入10斤的缸子,状态:10斤的有8斤,7斤的有0斤,3斤的有2斤。
第四步,将3斤的倒入7斤的。状态为:10斤的有8斤,7斤的有2斤,3斤的有0斤。
第五步,用10斤的缸子将3斤的缸子装满,10斤的缸子中正好剩余5(8-3)斤,将3斤缸子里的倒入7斤缸子里,也正好5斤。正好实现。
脑子瞬间不够用的我,给出了以下答案
import java.util.Arrays;
public class FiveFiveTool {
public static final int CUP0 = 3;
public static final int CUP1 = 7;
public static final int CUP2 = 10;
private String[] log ={"[0, 0, 10]"};
public int[][] startCompute(int[][] steps) {
//迭代出所有可能性
for (int i = 0; i < 100; i++) {
for (int j = 0; j < steps.length; j++) {
steps = changeWaterOnce(steps,j,2,0);
steps = changeWaterOnce(steps,j,2,1);
steps = changeWaterOnce(steps,j,1,2);
steps = changeWaterOnce(steps,j,1,0);
steps = changeWaterOnce(steps,j,0,1);
steps = changeWaterOnce(steps,j,0,2);
}
}
// 打印结果集
// for (int i = 0; i < steps.length; i++) {
// System.out.print(Arrays.toString(steps[i]));
// }
// System.out.println("");
System.out.println(log[log.length-1]);
return steps;
}
private int[][] changeWaterOnce(int[][] steps, int num, int fromCup, int toCup) {
if (num>=steps.length) {
return steps;
}
int[] result;
result = changeWater(steps[num], fromCup, toCup);
//若一维数组不存在,则添加到尾部
boolean haveSame = false;
for (int j = 0; j < steps.length; j++) {
if(Arrays.equals(result, steps[j])) {
haveSame = true;
}
}
int[][] results;
if (haveSame == false) {
results = Arrays.copyOf(steps, steps.length + 1);
results[results.length - 1] = result;
log = Arrays.copyOf(log, log.length+1);
log[log.length - 1] = log[num] + " "
+ fromCup + "-->" + toCup + ":\n"
+ "[" + results[results.length-1][0] + ", "
+ results[results.length-1][1] + ", "
+ results[results.length-1][2] + "]";
}else {
results=steps;
}
// //打印结果
// for (int j = 0; j < results.length; j++)
// System.out.print(Arrays.toString(results[j]));
// System.out.println("");
return results;
}
private int[] changeWater(int[] step, int fromCup, int toCup) {
int toCupEmpty = 0;
switch (toCup) {
case 0:toCupEmpty = CUP0 - step[0];break;
case 1:toCupEmpty = CUP1 - step[1];break;
case 2:toCupEmpty = CUP2 - step[2];break;
default:
System.out.println("当前倒入杯子存在问题:toCup=" + toCup);
break;
}
// 倒入杯子已满!
if (toCupEmpty < 1) {
return step;
}
int fromWater = 0;
int toWater = 0;
// 倒满
if (step[fromCup] >= toCupEmpty) {
fromWater = step[fromCup] - toCupEmpty;
toWater = step[toCup] + toCupEmpty;
}
// 全部倒入
if (step[fromCup] < toCupEmpty) {
fromWater = 0;
toWater = step[toCup] + step[fromCup];
}
int[] result = {step[0], step[1], step[2]};
switch (toCup) {
case 0:result[0] = toWater;break;
case 1:result[1] = toWater;break;
case 2:result[2] = toWater;break;
default:
System.out.println("当前倒入杯子存在问题:toCup=" + toCup);
break;
}
switch (fromCup) {
case 0:result[0] = fromWater;break;
case 1:result[1] = fromWater;break;
case 2:result[2] = fromWater;break;
default:
System.out.println("当前导出杯子存在问题:fromCup=" + fromCup);
break;
}
return result;
}
}
public class FiveFive {
private static FiveFiveTool tool;
private static int steps[][] = {{0,0,10}};
public static void main(String[] args) {
tool = new FiveFiveTool();
steps = tool.startCompute(steps);
}
}
输出:
[0, 0, 10] 2-->1:
[0, 7, 3] 1-->0:
[3, 4, 3] 0-->2:
[0, 4, 6] 1-->0:
[3, 1, 6] 0-->2:
[0, 1, 9] 1-->0:
[1, 0, 9] 2-->1:
[1, 7, 2] 1-->0:
[3, 5, 2] 0-->2:
[0, 5, 5]
源码下载
https://img-blog.csdn.net/20171010093243370
临时写的,不当之处,还望指教
其他智力题http://blog.csdn.net/qq_23370223/article/category/7197980