【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 ,旨在入门数据结构与算 ...
随机推荐
- 阿里云k8s服务springboot项目应用升级时出现502错误
背景 随着小步快跑.快速迭代的开发模式被越来越多的互联网企业认同和采用,应用的变更.升级频率变得越来越频繁.为了应对不同的升级需求,保证升级过程平稳顺利地进行,诞生了一系列的部署发布模式. 停机发布 ...
- [转载]Linux下终端字体颜色设置方法
原文地址:Linux下终端字体颜色设置方法作者:router 网上类似的文章有很多,但是都是转来转去的,没有经过测试,按照很多文章的方法会造成你设置之后的终端在换行和删除输入字符时终端显示会乱七八糟, ...
- SqlServer安装时的选项说明
转自:https://blog.csdn.net/m0_37154839/article/details/80233446 看看组件的功能说明吧 服务器组件 说明 SQL Server 数据库引擎 S ...
- Asynchronous and non-Blocking I/O 翻译[收藏好文]
http://www.tornadoweb.org/en/stable/guide/async.html Real-time web features require a long-lived mos ...
- du命令解决linux磁盘空间满的问题(很不错的哦)
首先你要确定是不是真正的是因为数据空间占满磁盘,经常是因为某个程序的日志占满了空间.当发现磁盘满了以后不要着急,使用以下命令从根目录开始排除查找哪个文件夹最大: du --max-depth=1 找到 ...
- php如何在某个时间上加一天?一小时? 时间加减(转)
<?php date_default_timezone_set('PRC'); //默认时区 echo "今天:",date("Y-m-d",time() ...
- 【C#】Skip和Tack方法实现分页
int pageIndex = SearchModel.PageIndex <= 0 ? 1 : SearchModel.PageIndex; return BatchInfoList.Skip ...
- asp.net C#取Excel 合并单元格内容
asp教程.net c#取excel 合并单元格内容读取excel数据,填充dataset// 连接字符串 string xlspath = server.mappath("~/www.11 ...
- 《TCP/IP详解卷1:协议》读书笔记
<TCP/IP详解卷1:协议>读书笔记 - QingLiXueShi - 博客园https://www.cnblogs.com/mengwang024/p/4425834.html < ...
- sqlserver使用存储过程发送http请求
本文主要向大家介绍了SQLServer数据库访问发送Http请求,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助. -- 通用读取获取数据存储过程 --开启Sql Serve ...