原题:
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 are

fill A
fill B
empty A
empty B
pour A B
pour B A
success
where pour A B" meanspour 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

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

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.
Sample Input

3 5 4
5 7 3
Sample Output

fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
大意:
给你两个壶a,b还有一个目标水量c。现在让你用a,b和无限的水倒出目标水量。其中a,b互质。

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<fstream>
#include <climits>
using namespace std;
//fstream input,output;int main()
{ios::sync_with_stdio(false);int ja,jb,aim;int a,b;while(cin>>ja>>jb>>aim){if(ja==aim){cout<<"fill A"<<endl;cout<<"success"<<endl;continue;}if(jb==aim){cout<<"fill B"<<endl;cout<<"success"<<endl;continue;}a=b=0;while(b!=aim){if(a==0){cout<<"fill A"<<endl;a=ja;}if(b<jb){cout<<"pour A B"<<endl;if(b+ja>jb){a=ja-(jb-b);b=jb;}else{b+=a;a=0;}}if(b==jb){cout<<"empty B"<<endl;b=0;cout<<"pour A B"<<endl;b=a;a=0;}}cout<<"success"<<endl;}
//  input.close();
//  output.close();return 0;
}

解答:
很经典的一个问题,刚读题的时候还以为让我判断能否倒出目标水量来,结果却是让我求操作方法。
首先先说明一下对任意给定的两个数a,b能否实现操作出目标c。现在假设b大于a,首先让a里的水装满不断倒进b中,直到b中的水装满。此时a里剩下的水是x,再把b里的水倒空,把x装进b中,不断重复就能发现这是一个经典的操作——更相减损的方法。也就是求最大公约数的方法。所以只要c的数量能够除开a与b的最大公约数就能够倒出目标水量,同时要求目标水量要小于b。

读完题后纠结了好长时间如何处理最少操作步骤,设a往b的里装水操作x,b往a里装水是y。则能得到,ax+by=1这样一个方程,再利用扩展欧几里得公式求出一组x,y的解,然后求得最小的x或者y….
后来想不出程序该怎么写了,看别人的题解居然发现任意一组解就可以….CNM的!

uva 571 Jugs相关推荐

  1. uva 571 - Jugs

    题目链接:uva 571 - Jugs 题目大意:给出A,B和aid,A表示小杯的容量,B表示大杯的容量,aid表示要求大杯中剩余的水量,有无限的水,给出方案,(A和B互质) 解题思路:A和B互质就说 ...

  2. layui table 表头合并_layui 动态表格之合并单元格

    需求: 下面用excel表格大概模拟下需求,左边是原来的,要改成右边这样的: ①第一步:再生成表格后调用此方法,以合并重复的单元格 done : function(res, curr, count) ...

  3. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  4. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  5. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  7. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  8. UVa 11174 - Stand in a Line

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVa 10112 - Myacm Triangles

    UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...

最新文章

  1. Magic Leap大举进军AR医疗:新品率先与医疗机构合作,挖走HoloLens老客户
  2. linux 配置DNS正反区域,Linux基础服务_DNS原理以及正反向DNS配置
  3. centos mysql 5.6.19_Centos5.8 安装 MySQL5.6.19
  4. 讲解web服务所涉及到的重要知识点
  5. 基于基站定位数据的商圈分析代码详细解释
  6. 如何在开源社区贡献代码_如何在15分钟内从浏览器获得您的第一个开源贡献
  7. C++学习之路 | PTA乙级—— 1015 德才论 (25分)(精简)
  8. datatable使用groupby进行分组统计 [2]
  9. react native ios 上架
  10. /etc/securetty文件
  11. 监控摄像头接入流媒体服务器的几种方式
  12. Android 官方命令深入分析之android
  13. php 7 中对数值 * 100 出现很多小数_PHP快速入门第二讲:数据类型
  14. 从备用类型总盗用steal page
  15. SpringMVC+Spring Data JPA +Bootstrap 分页实现和模糊查询分页
  16. android麦克风消回音处理,【技术帖】解决麦克风回音重的难题
  17. matlab结构数组增加域,如何用matlab创建结构数组
  18. 电路设计_RS485总线典型电路介绍
  19. PL/0词法分析程序
  20. 【双清/双wipe】使用adb命令进行双清/双wipe

热门文章

  1. 利用1.1.1.1进行DNS网络加速,仅需2分钟让网络更快
  2. 1060驱动java_ubuntu16.04 安装 GTX 1060 显卡驱动
  3. macOS Monterey 如何设置PPT演讲者模式实现电脑看备注投影看播放内容
  4. openstack中 Server Error for url: http://controller:9696/v2.0/agents, Internal Server Error
  5. 云服务器搭建深度学习环境
  6. 安徽省大数据与人工智能竞赛经验分享-4【从赛题角度看人员分工】
  7. ckc交易什么意思_股票熔断是什么意思啊?熔断机制对股民的影响有那些
  8. STM32 定时器编码器模式时,如何理解编码器计数
  9. win10关机后自动重启_电脑自动关机或重启的解决办法
  10. 计算机关机慢怎么解决方法,电脑关机很慢,详细教您win7电脑关机很慢的解决方法...