【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 ,旨在入门数据结构与算 ...
随机推荐
- java2小结(草稿)
Struts2 Servlet 小的Java程序,运行在服务器端,接收和响应从客户端发送过来的请求 流程分析: Servlet生命周期? Servlet配置自动加载?(理解) 1.服务器在启动的时候, ...
- DPDK架构与特点(转)
from:http://www.cnblogs.com/mylinuxer/p/4277676.html DPDK架构与特点 当年在某公司实习的时候,当时老大给了我一份DPDK的文档,说是将来很有用, ...
- JAVA删除文件及文件夹
JAVA在删除文件或文件夹时,在java.io.File类下有个delete的方法,并且可以返回true or false, 用这个方法来删除单个文件时,很好使,但在删除文件夹时,如果文件夹下面有文件 ...
- lamp环境服务器配置文档
服务器配置命令开始(蓝色为输入命令,灰色为反馈内容): Yum update Reboot; yum -y install mysql mysql-server mysql-devel php php ...
- TextSharp详情
TextSharp是一个生成Pdf文件的开源项目,最近在项目中有使用到这个项目,对使用中的经验作一个小结. ITextSharp中相关的概念: 一.Document 这个对象有三个构造函数: 隐藏行号 ...
- thinkphp日志分析
#!/usr/bin/perl -w use strict; use warnings; use Tie::File; #### # Thinkphp日志分析 # 日志基本格式:{$now} &quo ...
- 豆瓣上9分以上的IT书籍-编程语言篇
我当要学习某些技术时,第一时间就是去找相关的书籍.而豆瓣读书是我主要的参考依据,主要是它的评分基本比较靠谱,对于技术书籍,一般来说评分在8分以上就是不错的书籍了,而达到9分就可以列入"必读& ...
- Android HttpURLConnection源代码分析
Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...
- [Android] 使用Include布局+Fragment滑动切换屏幕
前面的文章已经讲述了"随手拍"项目图像处理的技术部分,该篇文章主要是主界面的布局及屏幕滑动切换,并结合鸿洋大神的视频和郭神的第一行代码(强推两人Android博客),完毕了 ...
- Openssl aes加解密例程
原文链接: http://blog.csdn.net/itmes/article/details/7714854 假设我们已经下载了 openssl的源码,并成功编译,设置好了编程环境. 我们现在来看 ...