KY261 Jugs

题目地址

相对素数:不能被所有给定的整数(除了0)整除的数

题目描述:

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps arefill A fill B empty A empty B pour A B pour B A successwhere "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.You may assume that the input you are given does have a solution.

输入:

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

输出:

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

分析:
A B 两个罐子,无限水,求得到指定体积水的方法步骤

思路:
显然,可以倒出的水体积是:A B 的最大公因数的倍数
Step1:如果当前A满了,清空A
Step2:如果当前B是空,倒满B
Step3:把B的水倒入A(假定A体积小于等于B体积)
Step4:如果当前A有水,但是不满,尽可能把B中水往A里面倒
不断循环Step1~Step4,直到A==N || B == N 的时候,就结束

代码:

#include<iostream>
#include<string>
#include<vector>using namespace std;int main(){int ca,cb,n;while (cin>>ca>>cb>>n){//初始化罐子内水的体积int a=0,b=0;while (!(a==n||b==n||a+b==n)){if(a==ca){//A满了,倒空a=0;cout<<"empty A"<<endl;}else if(b==0){//B空了,倒满b=cb;cout<<"fill B"<<endl;}else{//A没满,B没空,持续性的将B倒入A//B中的水:要么可以一次性倒满A,要么不可以,要考虑到A中有水但不满的情况if(a+b<=ca){a=a+b;b=0;}else{b=b-(ca-a);a=ca;}cout<<"pour B A"<<endl;   }}cout<<"success"<<endl;     }}

问题:
本算法只考虑了从B往A倒水的情况,实际上还可以从A往B倒水

KY261 Jugs相关推荐

  1. UVA571 - Jugs(数论)

    UVA571 - Jugs(数论) 题目链接 题目大意:给你A和B的水杯.给你三种操作:fill X:把X杯里面加满水.empty X:把X杯中的水清空.pour X Y 把X的水倒入Y中直到一方满或 ...

  2. 问题 A: Jugs

    时间限制: 1 Sec  内存限制: 32 MB 提交: 288  解决: 0 [提交][状态][讨论版][命题人:外部导入] 题目描述 In the movie "Die Hard 3&q ...

  3. c语言大小写字母互换1005,1005 Jugs,1005jugs

    1005 Jugs,1005jugs 辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. 1 #include 2 3 intmain(){4 intA,B,T,sA,sB;5 while(sca ...

  4. ZOJ Problem 1005 jugs

    题目 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the ...

  5. Codeup100000609问题 A: Jugs

    题目描述: In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with t ...

  6. 扩展欧几里德算法解决问题A:Jugs

    Jugs codeup的Jugs题目需要使用扩展欧几里德算法解决: Zoj的jugs题目需要使用BFS算法解决: codeup的Jugs题目需要使用扩展欧几里德算法解决: 题目链接:http://co ...

  7. zoj 1005 Jugs BFS

    感想:这是我的第一道oj题,思路我想了很久,感觉建模能力还是不够强啊,理清楚了就好,把各个操作看成一条路,BFS就好 http://acm.zju.edu.cn/onlinejudge/showPro ...

  8. uva 571 Jugs

    原题: In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the ...

  9. poj1066 Jugs

    poj1066 Jugs http://poj.org/problem?id=1606 解题思路:本题可以用数学方法解得,最易理解,常规的解法是搜索.直接用接近模拟的广度优先搜索即可过. 给两个容器, ...

最新文章

  1. git fetch -p 获取远程仓库的新分支以及删除远程仓库已删除的分支
  2. Fiddler之弱网测试(Web)
  3. java 成员类_Java类的五大成员之一——内部类
  4. 周五话营销 | 健身房花式卖卡,诠释点击营销流
  5. 在eclipse及myEclipse下安装插件之方法
  6. makefile中的@ $ :
  7. [转]Android中pendingIntent的深入理解
  8. 二狗叫你制作千M网线的线序及方法
  9. python input 拖入路径 去除转义 空格_python学习笔记(基础-2)(转载)
  10. linux源码下载阿帕奇,Ubuntu 12.04下源码安装Apache
  11. php如何解决报错,php 启动报错如何解决_PHP教程
  12. 数据中心节能制冷---冷却塔帮忙
  13. Web Hacking 101 中文版 十五、代码执行
  14. C++11 Unicode 支持
  15. Android启动过程概述
  16. 每日java制作小工具_制作Java小工具并在命令行中执行
  17. 数据科学和人工智能技术笔记 十九、数据整理(下)
  18. vue上传、修改头像
  19. 大数据框架之Spark详解
  20. 基因组+转录组助力油菜种子油含量自然变异的遗传研究

热门文章

  1. 产品设计如何鼓励用户上传头像?
  2. 网页微信公众平台登录电脑版
  3. Serverless 极致弹性解构在线游戏行业痛点,你有过迷茫吗
  4. Ubuntu 20.04安装GTX 1060显卡驱动+cuda 11.4 + cudnn 8,nvidia-smi 报错:NVIDIA-SMI has failed
  5. 硬盘柱面损坏怎么办_硬盘0柱面损坏数据恢复(老牌数据恢复)
  6. 快递100接口使用整理
  7. 阿里ET大脑如何帮助养猪产业提升PSY从20到32?
  8. 系统之美 作者:德内拉梅多斯
  9. 【elementUI】el-table树形结构样式修改-gif展示说明 按要求自取即可
  10. 简述你对人工智能未来发展的看法?