算法分析之量水问题

2023-11-29 08:40
文章标签 算法 分析 问题 之量

本文主要是介绍算法分析之量水问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题描述

  • 有两个容器A和B,容积分别为a升和b升,有无限多的水,现在需要c升水。
  • 我们还有一个足够大的水缸C,足够容纳c升水。起初它是空的,我们只能往水缸里倒入水,而不能倒出。
  • 可以进行的操作是:
    <1>把一个容器灌满;
    <2>把一个容器清空(容器里剩余的水全部倒掉,或者倒入水缸)
    <3>用一个容器的水倒入另外一个容器,直到倒出水的容器空或者傲入水的容器满。
  • 问是否能够通过有限次操作,使得水缸c中最后恰好有c升水。
  • 输入:三个整数a, b, c,其中0<a, b, c<=10000
  • 输出:0或1,表示能否达到要求;如果能达到要求,则请给出具体的操作步骤序列。
    在这里插入图片描述

问题解决

1.确定问题是否有解

参考文章
要确定使用容器A和容器B,依照倒水规则进行倒水能否得到c升水,可以转化为
用A盛,倒B里,B满了,就倒掉。 最终 A盛M次,B盛N次。结果c = aM -bN。
或者是反过来c = bN - aM,即求|aM - bN| = c.
要使|aM - bN| = c,且a,b,c都是大于零的整数,只要求a和b的最大公因数,是否可以被c整除,就可以确定问题是否有解了。

2.给出具体的操作步骤序列

确定问题有解,接下来就是得到具体的操作步骤序列
问题中c,b,c三个值会有三种情况。
1.c >= a+b ,水缸中还需倒入的水的体积 大于等于大容器和小容器的体积和
2.c >= a && c < a+b,水缸中还需倒入的水的体积 小于大容器和小容器的体积和 且 大于大容器的体积
3.c > 0 && c < a,水缸中还需倒入的水的体积 小于大容器的体积
第1、第2种情况比较好解决,就是尽可能使用整个容器,进行倒水。
情况3可以通过两个容器间相互倾倒得到,比较复杂。
参考文章
解决:以量杯作为树的结点,在树中找出一条路径( 分支 ),使得路径终结点的当前水量为c升,那么问题就得到解决。把以下六个操作作为子结点 .求解的过程就是在这棵树中找到一条路径.使得路径终结点处,两个容器任意一个容器中的水为c升.
对容器的六种操作

  • A->B 表A容器倒入B容器
  • B->A 表B容器倒入A容器
  • A->E 表将A容器的水倒空
  • B->E 表将B容器的水倒空
  • F->A 表将A容器倒满
  • F->B 表将B容器杯倒满
3.代码实现
import java.util.*;public class Solution1 {//大容器全称static String bigC = "【容器A】";//小容器全称static String smallC = "【容器B】";//水缸全称static String vat = "【水缸C】";//水缸C中此时水的体积static int curC;// m*a + n*b = cpublic static void main(String[] args) {Scanner input = new Scanner(System.in);int a, b, c;System.out.println("请输入容器A的容量:");a = input.nextInt();System.out.println("请输入容器B的容量:");b = input.nextInt();System.out.println("请输入水缸的容量:");c = input.nextInt();if (!(a > 0 && a < 10001 && b > 0 && b < 10001 && c > 0 && c < 10001)) {System.out.println("输入的数超出范围");System.exit(1);}if (!hasR(a, b, c)) {System.out.println(0);} else {System.out.println(1);getRes(a, b, c);}System.out.print(">>>>>>>>>>>>>>结束>>>>>>>>>>>>>>");}public static void getRes(int a, int b, int c) {bigC = a >= b ? bigC : smallC;smallC = bigC.equals("【容器A】") ? "【容器B】" : "【容器A】";if (a < b) {int temp = a;a = b;b = temp;}if (c >= (a + b)) {//情况一:c > a+bsituation1(a, b, c);} else if (c >= a && c < (a + b)) {//情况二:c >= a && c < a+bsituation2(a, b, c);} else {//情况三:c > 0 && c < asituation3(a, b, c);}}// 情况一: c >= a+b ,水缸中还需倒入的水的体积 大于等于大容器和小容器的体积和public static void situation1(int a, int b, int c) {System.out.println(">>>>>>>>>>>>>>情况一开始>>>>>>>>>>>>>>");//重复的次数(将大容器和小容器都装满,倒入水缸中的次数)int count = c / (a + b);curC += (count * (a + b));System.out.println("往" + bigC + "和" + smallC +"都倒满水后,倒入" + vat + "中,重复" +count + "次。此时" + vat + "中水的体积为" + curC + "升。");if (c % (a + b) != 0) {//还需倒入水缸中的水大小int reminder = c % (a + b);//c-count*(a+b)getRes(a, b, reminder);//考虑剩下的水如何倒(此时reminder肯定小于a+b,可能是情况二和情况三)}}//情况二:c >= a && c < a+b,水缸中还需倒入的水的体积 小于大容器和小容器的体积和 且 大于大容器的体积public static void situation2(int a, int b, int c) {System.out.println(">>>>>>>>>>>>>>情况二开始>>>>>>>>>>>>>>");//直接往水缸中倒入大容器体积的水curC += a;System.out.println("往容器" + bigC + "到满水后,倒入" + vat + "中,此时" + vat + "中水的体积为" + curC + "升。");if (c != a) {//c-a!=0//还需倒入水缸中的水大小int reminder = c - a;getRes(a, b, reminder);//考虑剩下的水如何倒(此时reminder肯定小于a,只可能是情况三)}}//情况三:c > 0 && c < a,水缸中还需倒入的水的体积 小于大容器的体积public static void situation3(int a, int b, int c) {System.out.println(">>>>>>>>>>>>>>情况三开始>>>>>>>>>>>>>>");//采用广度优先算法Queue<Node> queue = new LinkedList<>();Set<String> set = new HashSet<>();queue.add(new Node(0, 0, ""));int tempA, tempB;String mark;String process;while (!queue.isEmpty()) {int size = queue.size();for (int i = 0; i < size; i++) {Node node = queue.poll();//1.尝试将 容器A 中的水倒入容器 B (容器A不为空,容器B不为满)if (node.curA != 0 && node.curB != b) {tempA = node.curA + node.curB > b ? node.curA + node.curB - b : 0;tempB = Math.min(node.curA + node.curB, b);mark = tempA + "," + tempB;process = "将" + bigC + "中的水倒入" + smallC + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n";if (tempA == c) {System.out.print(node.processes + process);curC += tempA;tempA = 0;System.out.print("将" + bigC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (tempB == c) {System.out.print(node.processes + process);curC += tempB;tempB = 0;System.out.print("将" + smallC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (!set.contains(mark)) {//是否之前出现过set.add(mark);queue.add(new Node(tempA, tempB, node.processes + process));}}//2.尝试将 容器B 中的水倒入容器 A (容器B不为空,容器A不为满if (node.curB != 0 && node.curA != a) {tempA = Math.min(node.curA + node.curB, a);tempB = node.curA + node.curB > a ? node.curA + node.curB - a : 0;mark = tempA + "," + tempB;process = "将" + smallC + "中的水倒入" + bigC + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n";if (tempA == c) {System.out.print(node.processes + process);curC += tempA;tempA = 0;System.out.print("将" + bigC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (tempB == c) {System.out.print(node.processes + process);curC += tempB;tempB = 0;System.out.print("将" + smallC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (!set.contains(mark)) {//是否之前出现过set.add(mark);queue.add(new Node(tempA, tempB, node.processes + process));}}//3.尝试将 容器A 中的水倒空(容器A不为空)if (node.curA != 0) {tempA = 0;tempB = node.curB;mark = tempA + "," + tempB;process = "将容器" + bigC + "中的水倒空,此时" + vat + "中水的体积为" + curC + "升,容器"+ bigC + "中水体积为" + tempA + "升,容器" + smallC + "中水体积为" + tempB + "升。\n";if (tempA == c) {System.out.print(node.processes + process);curC += tempA;tempA = 0;System.out.print("将" + bigC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (tempB == c) {System.out.print(node.processes + process);curC += tempB;tempB = 0;System.out.print("将" + smallC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (!set.contains(mark)) {//是否之前出现过set.add(mark);queue.add(new Node(tempA, tempB, node.processes + process));}}//4.尝试将 容器B 中的水倒空(容器B不为空)if (node.curB != 0) {tempA = node.curA;tempB = 0;mark = tempA + "," + tempB;process = "将" + smallC + "中的水倒空,此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水体积为" + tempA + "升," + smallC + "中水体积为" + tempB + "升。\n";if (tempA == c) {System.out.print(node.processes + process);curC += tempA;tempA = 0;System.out.print("将" + bigC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (tempB == c) {System.out.print(node.processes + process);curC += tempB;tempB = 0;System.out.print("将" + smallC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (!set.contains(mark)) {//是否之前出现过set.add(mark);queue.add(new Node(tempA, tempB, node.processes + process));}}//5.尝试将 容器A 填满水(容器A不为满)if (node.curA != a) {tempA = a;tempB = node.curB;mark = tempA + "," + tempB;process = "将" + bigC + "用水填满,此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水体积为" + tempA + "升," + smallC + "中水体积为" + tempB + "升。\n";if (tempA == c) {System.out.print(node.processes + process);curC += tempA;tempA = 0;System.out.print("将" + bigC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (tempB == c) {System.out.print(node.processes + process);curC += tempB;tempB = 0;System.out.print("将" + smallC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (!set.contains(mark)) {//是否之前出现过set.add(mark);queue.add(new Node(tempA, tempB, node.processes + process));}}//6.尝试将 容器B 填满水(容器B不为满)if (node.curB != b) {tempA = node.curA;tempB = b;mark = tempA + "," + tempB;process = "将" + smallC + "用水填满,此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水体积为" + tempA + "升," + smallC + "中水体积为" + tempB + "升。\n";if (tempA == c) {System.out.print(node.processes + process);curC += tempA;tempA = 0;System.out.print("将" + bigC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (tempB == c) {System.out.print(node.processes + process);curC += tempB;tempB = 0;System.out.print("将" + smallC + "中的水倒入" + vat + ",此时" + vat + "中水的体积为" + curC + "升,"+ bigC + "中水的体积为" + tempA + "升," + smallC + "中水的体积为" + tempB + "升。\n");return;}if (!set.contains(mark)) {//是否之前出现过set.add(mark);queue.add(new Node(tempA, tempB, node.processes + process));}}}}}//自定义节点static class Node {//该节点位置的 大容器中水的体积curA,小容器中水的体积curB,以及过程描述processesint curA, curB;String processes;public Node(int curA, int curB, String processes) {this.curA = curA;this.curB = curB;this.processes = processes;}}//判断是否存在结果private static boolean hasR(int a, int b, int c) {int gcf = getGCF(a, b);if (c % gcf != 0) return false;return true;}//求两个数的最大公因数private static int getGCF(int a, int b) {for (int i = Math.min(a, b); i > 1; i--) {if (a % i == 0 && b % i == 0) {return i;}}return 1;}
}
4.实现效果

在这里插入图片描述

这篇关于算法分析之量水问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/432163

相关文章

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.