【Acm】算法之美—Jugs
题目概述: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升水,要求比较简单,就不复述了。
题目分析
简单分析下此题目:
两个水桶 A和B,则需要两个变量来表示其容量,设为 ca和cb
目标变量 n
水在倒的过程中,两个水桶的当前水量会发生变化,需要两个变量表示当前量,设为 x和y
目标量可能由 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的更多相关文章
- 【EatBook】-NO.2.EatBook.2.JavaArchitecture.1.001-《修炼Java开发技术在架构中体验设计模式和算法之美》-
1.0.0 Summary Tittle:[EatBook]-NO.2.EatBook.2.JavaArchitecture.1.001-<修炼Java开发技术在架构中体验设计模式和算法之美&g ...
- ACM,算法
ACM,算法 描述 最近Topcoder的XD遇到了一个难题,倘若一个数的三次方的后三位是111,他把这样的数称为小光棍数.他已经知道了第一个小光棍数是471,471的三次方是104487111,现在 ...
- 推荐学习《算法之美:指导工作与生活的算法》中文PDF+英文PDF
我们所有人的生活都受到有限空间和有限时间的限制,因此常常面临一系列难以抉择的问题.在一天或者一生的时光里,哪些事是我们应该做的,哪些是应该放弃的?我们对杂乱无序的容忍底线是什么?新的活动与熟悉并喜爱的 ...
- JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)
前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...
- JavaScript 数据结构与算法之美 - 十大经典排序算法汇总(图文并茂)
1. 前言 算法为王. 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手:只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 ...
- JavaScript 数据结构与算法之美 - 栈内存与堆内存 、浅拷贝与深拷贝
前言 想写好前端,先练好内功. 栈内存与堆内存 .浅拷贝与深拷贝,可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScri ...
- JavaScript 数据结构与算法之美 - 冒泡排序、插入排序、选择排序
1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...
- JavaScript 数据结构与算法之美 - 归并排序、快速排序、希尔排序、堆排序
1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...
- JavaScript 数据结构与算法之美 - 桶排序、计数排序、基数排序
1. 前言 算法为王. 想学好前端,先练好内功,只有内功深厚者,前端之路才会走得更远. 笔者写的 JavaScript 数据结构与算法之美 系列用的语言是 JavaScript ,旨在入门数据结构与算 ...
随机推荐
- ios中radiobutton
#import <UIKit/UIKit.h> @protocol RadioButtonExtDelegate; @interface RadioButtonExt : UIView - ...
- socket 995 错误 boost
这个错误的中文解释是:由于线程退出或应用程序请求,已中止 I/O 操作. 最近几天学习boost asio 在抄官方的一个实例代码时遇到 了,这个错误搞了我三天才解决,就是在一行代码中少了一个 s 所 ...
- SpringBoot配置属性之Migration
SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...
- 【java】详解JDK的安装和配置
目录结构: contents structure [+] 什么是JDK JDK的三个版本 JDK包含的主要内容 JDK的安装 JDK的配置 配置JAVA_HOME 配置PATH 到底自己需不需要配置C ...
- 探讨android更新UI的几种方法
作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因为现实是残酷的,就算一开始的时间和精力非常充足,也会随着项目的推进而逐步消磨殆尽.我们会发现,自己越来越 ...
- iOS runtime执行时具体解释
什么是runtime? runtime直译就是执行时间,run(跑,执行) time(时间),网上大家都叫它执行时,它是一套比較底层的纯C语言API,属于一个C语言库,包括了非常多底层的C语言API, ...
- 15. 使用Apache Curator装饰ZooKeeper
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
- linux下配置tomcat集群的负载均衡
linux下配置tomcat集群的负载均衡 一.首先了解下与集群相关的几个概念集群:集群是一组协同工作的服务实体,用以提供比单一服务实体更具扩展性与可用性的服务平台.在客户端看来,一个集群就象是一个服 ...
- Redis命令汇总
设置服务后台启动 cd /usr/local/redisview redis.conf 将daemonize no改为 daemonize yes保存退出 启动:./reids-server redi ...
- Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载)
源码:http://files.cnblogs.com/android100/StandardCamera2013-10-18.zip 废话不说了,就是加个seekbar,拖动的话能够调节焦距,让画面 ...