10斤酒两个桶

题目:

有三口酒缸,分别能装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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Windows 10中,可以使用桥接功能来连接两个不同的网络接口卡,并实现网络加速的效果。下面是详细的操作步骤: 步骤1:确保计算机上已经安装了两个网络接口卡,并且都已经正确连接。 步骤2:在任务栏上的搜索框中输入“控制面板”,点击打开控制面板。 步骤3:在控制面板中,选择“网络和 Internet”选项。 步骤4:在“网络和 Internet”选项中,选择“网络和共享中心”。 步骤5:在“网络和共享中心”界面中,点击左侧导航栏中的“更改适配器设置”。 步骤6:在“网络连接”窗口中,选择两个需要桥接的网络接口卡(通过按住Ctrl键选择多个接口卡)。 步骤7:右键点击所选的接口卡,选择“桥接连接”选项。 步骤8:等待一段时间,直到“桥接连接”创建完毕。此时,两个网络接口卡将通过桥接方式连接起来,形成一个虚拟的网络接口。 步骤9:重新启动计算机,以使桥接生效。 桥接完成后,两个网络接口卡之间的数据传输速率将会加速,并且可以共享网络连接。通过桥接,可以将两个不同网络供应商的带宽进行合并使用,从而增加网络传输的速度和带宽。此外,也可以在有需要的情况下,通过桥接实现不同网络之间的互通,方便数据的共享和传输。 需要注意的是,在使用桥接功能时,需要确保网络接口卡的驱动程序是最新的,并且网络连接的稳定性良好。如果在操作过程中遇到任何问题,可以参考相关的网络设置教程或者咨询网络工程师的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值