题目概述:Jugs

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:

    • you can fill a jug,

    • you can empty a jug

    • 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" 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

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


简单描述

题目意思还是比较简单,简单概述一下就是:有无限的水和两个水桶,一个容量 ca升,一个容量 cb升,问怎么用两个桶得到 n升水,要求比较简单,就不复述了。


题目分析

简单分析下此题目:

    1. 两个水桶 A和B,则需要两个变量来表示其容量,设为 ca和cb

    2. 目标变量 n

    3. 水在倒的过程中,两个水桶的当前水量会发生变化,需要两个变量表示当前量,设为 x和y

    4. 目标量可能由 A得到,也可能由 B得到,所以需要对 A和B进行判断


解题算法

注释丰富,就不多说了

  

 #include < stdio.h>
int main()
{
int ca,cb,n,x,y;
/* Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal.
* 即:ca为 A的容量,cb为 B的容量,N为要得到的水
* x为 ca当前已有的水
* y为 cb当前已有的水
*/
while( scanf("%d %d %d",&ca,&cb,&n)!=EOF)
{
x=y=;
while()
{
printf("fill A\n");
x=ca;
if(x==n) /* 如果 A得到目标 */
{
printf("success\n");
break; /* 成功,退出循环 */
}
while(x>)
{
if((cb-y)>=x) /* B能够容纳的水 > A当前的水 */
{
printf("pour A B\n"); /* 则将 A中的水全部倒入 B中 */
y=y+x; /* y增加 x升水 */
x=;
}
else
{
printf("pour A B\n");
x=x-(cb-y); /* A中还剩有水 */
y=cb; /* B中装满 */
}
if(x==n) /* A得到目标 */
{
printf("success\n");
break;
}
if(y==n) /* B得到目标 */
break;
if(y==cb) /* B空 */
{
printf("empty B\n");
y=;
}
}
if(x==n)
break;
if(y==n)
{
printf("success\n");
break;
}
}
}
return ;
}

【Acm】算法之美—Jugs的更多相关文章

  1. 【EatBook】-NO.2.EatBook.2.JavaArchitecture.1.001-《修炼Java开发技术在架构中体验设计模式和算法之美》-

    1.0.0 Summary Tittle:[EatBook]-NO.2.EatBook.2.JavaArchitecture.1.001-<修炼Java开发技术在架构中体验设计模式和算法之美&g ...

  2. ACM,算法

    ACM,算法 描述 最近Topcoder的XD遇到了一个难题,倘若一个数的三次方的后三位是111,他把这样的数称为小光棍数.他已经知道了第一个小光棍数是471,471的三次方是104487111,现在 ...

  3. 推荐学习《算法之美:指导工作与生活的算法》中文PDF+英文PDF

    我们所有人的生活都受到有限空间和有限时间的限制,因此常常面临一系列难以抉择的问题.在一天或者一生的时光里,哪些事是我们应该做的,哪些是应该放弃的?我们对杂乱无序的容忍底线是什么?新的活动与熟悉并喜爱的 ...

  4. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  5. JavaScript 数据结构与算法之美 - 十大经典排序算法汇总(图文并茂)

    1. 前言 算法为王. 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手:只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 ...

  6. JavaScript 数据结构与算法之美 - 栈内存与堆内存 、浅拷贝与深拷贝

    前言 想写好前端,先练好内功. 栈内存与堆内存 .浅拷贝与深拷贝,可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScri ...

  7. JavaScript 数据结构与算法之美 - 冒泡排序、插入排序、选择排序

    1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...

  8. JavaScript 数据结构与算法之美 - 归并排序、快速排序、希尔排序、堆排序

    1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...

  9. JavaScript 数据结构与算法之美 - 桶排序、计数排序、基数排序

    1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...

随机推荐

  1. NSDictionary的分类

    @implementation NSDictionary (extra) //根据key值的到字典中的object - (id)getObjectByKey:(NSString*)key { NSAr ...

  2. Git提交代码报错Git push error:src refspec XXX matches more than one解决方案

    Git提交代码push时,报错这个 error: src refspec master matches more than one. error: failed to push some refs t ...

  3. Node单线程高并发原理

    一.node是如何处理web请求的 浏览器中的js是单线程的,node也是单线程的.这个单线程相当于一个大管家,一切大小事务都要经过他的手才能办成,它总是把IO任务放入到任务池中. 虽然说是单线程,但 ...

  4. jenkins+maven+git+ 邮件自动转发 持续化集成 图文教程

    1.所需要的插件,安装plugin ,进入mangae Jenkins→ manage Plugins, 切换到Available tab, 选择如下plugin 安装 Gitplugin, GitH ...

  5. ROS学习(十一)—— msg srv

    一.msg 和 srv介绍 1.定义 消息(msg): msg文件就是一个描述ROS中所使用消息类型的简单文本.它们会被用来生成不同语言的源代码 服务(srv): 一个srv文件描述一项服务.它包含两 ...

  6. 【SQL】SQL中Case When的用法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...

  7. 【java】自定义异常类

    目录结构: contents structure [+] 为什么需要自定义异常类 自定义异常的方式 实例 日常日志 一,为什么需要自定义异常类 当java中的异常类型没有能够满足我们所需的异常的时候就 ...

  8. JDBC连接SQLServer出现的异常

    数据库连接.  question1. java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver 异常 ...

  9. Java读取excel的示例

    一.引用的jar包,apache的POI // https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml compile group: ' ...

  10. How to get current timestamps in Java

    How to get current timestamps in Java Timestamp timestamp = new Timestamp(System.currentTimeMillis() ...