时间限制:1 秒

内存限制:32 兆

特殊判题:是

提交:243

解决:200

题目描述:

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

样例输入:
3 5 4
5 7 3
样例输出:
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
来源:
2009年北京大学计算机研究生机试真题

思路:

给两个罐子,互相倒水得到规定的体积。此类题解法众多,各有各的思路,网上也能搜到一大片,不多解释了。

代码:

#include <stdio.h>

int ca, cb;
int a, b; void fill(char c)
{
if (c == 'A')
a = ca;
else
b = cb;
printf("fill %c\n", c);
} void pour(char c1, char c2)
{
if (c1 == 'A')
{
if (a+b >= cb)
{
a -= (cb-b);
b = cb;
}
else
{
b += a;
a = 0;
}
}
else
{
if (a+b >= ca)
{
b -= (ca-a);
a = ca;
}
else
{
a += b;
b = 0;
}
}
printf("pour %c %c\n", c1, c2);
} void empty(char c)
{
if (c == 'A')
a = 0;
else
b = 0;
printf("empty %c\n", c);
} int main(void)
{
int n; while (scanf("%d%d%d", &ca, &cb, &n) != EOF)
{
a = b = 0;
while (a != n && b != n)
{
fill('B');
pour('B', 'A');
while (b != 0 && a != n && b != n)
{
empty('A');
pour('B', 'A');
}
}
printf("success\n");
} return 0;
}
/**************************************************************
Problem: 1147
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/

九度OJ 1147:Jugs(罐子) (模拟、游戏)的更多相关文章

  1. 九度OJ 1179 阶乘(模拟)

    题目1179:阶乘 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4526 解决:1315 题目描写叙述: 输入n, 求y1=1!+3!+...m!(m是小于等于n的最大奇数) y2=2! ...

  2. 九度OJ 1177 查找 (模拟)

    题目1177:查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5659 解决:1667 题目描写叙述: 读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共同拥有 ...

  3. 九度OJ 打印日期 (模拟)

    题目1186:打印日期 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4284 解决:1483 题目描写叙述: 给出年分m和一年中的第n天,算出第n天是几月几号. 输入: 输入包含两个整数 ...

  4. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  5. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  6. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  7. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  8. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  9. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

随机推荐

  1. VirtualBox 扩展虚拟硬盘容量

    转载:VirtualBox 扩展虚拟硬盘容量 如果使用的是ubuntu主机加xp虚拟机,扩容后,xp还无法识别扩大后的硬盘部分,可以在xp下使用“分区助手”进行处理,即将扩大的空间分给C盘.

  2. gitbook简单安装和使用

    1.gitbook是用来写书的,支持markdown等 2.在线写 gitbook最新版地址:https://www.gitbook.com/account/ 免费用户只能有一个public和一个pr ...

  3. 执行sudo命令时的提示语,如何修改?

    如图所示,执行sudo命令,提示语(有中文和英文两个版本): 上面的提示内容是sudo软件原生的内容. 使用下面的方法,有的时候是可行的.sudo -p '提示语' 命令 如果要修改sudo软件原生的 ...

  4. SilverLight.3-Validation:二、银光验证。TheLabel、TheDescriptionViewer和TheValidationSummary

    ylbtech-SilverLight.3-DataControls_BetterDataFroms:二.银光验证.TheLabel.TheDescriptionViewer和TheValidatio ...

  5. windows下用vscode写C++

    [本文参考:https://www.cnblogs.com/zhuzhenwei918/p/9057289.html  和 https://www.zhihu.com/question/3031589 ...

  6. 转: Linux下使用java -jar运行可执行jar包的正确方式

    from:  http://codepub.cn/2016/05/11/The-correct-way-to-use-java-jar-run-an-executable-jar-package-un ...

  7. AutoCAD如何将dwf转成dwg格式

    dwf转成dwg怎么转, 悬赏分:30 - 解决时间:2009-11-22 10:19 重金:dwf转成dwg怎么转, 我是用在出图上的. 最佳答案 Design Web Format (DWF) 文 ...

  8. hibernate映射排序

    @OneToMany(mappedBy="member") @OrderBy(value = "TousuID desc")

  9. jQuery与ajax的应用(一)

    <body> <div id="resText"></div> <div id="reshtml"></d ...

  10. Android UI开源框架

    1.Side-Menu.Android 分类側滑菜单,Yalantis 出品. 项目地址:https://github.com/Yalantis/Side-Menu.Android 2.Context ...