九度OJ 1147:Jugs(罐子) (模拟、游戏)
时间限制: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
successwhere "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
思路:
给两个罐子,互相倒水得到规定的体积。此类题解法众多,各有各的思路,网上也能搜到一大片,不多解释了。
代码:
#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(罐子) (模拟、游戏)的更多相关文章
- 九度OJ 1179 阶乘(模拟)
题目1179:阶乘 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4526 解决:1315 题目描写叙述: 输入n, 求y1=1!+3!+...m!(m是小于等于n的最大奇数) y2=2! ...
- 九度OJ 1177 查找 (模拟)
题目1177:查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5659 解决:1667 题目描写叙述: 读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共同拥有 ...
- 九度OJ 打印日期 (模拟)
题目1186:打印日期 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4284 解决:1483 题目描写叙述: 给出年分m和一年中的第n天,算出第n天是几月几号. 输入: 输入包含两个整数 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
随机推荐
- HDU5877Weak Pair
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 26 ...
- 某考试 T2 Tree
2 树 2.1 题目描述 给一棵n 个节点的树,节点分别编号为0 到n - 1.你可以通过如下的操作来修改这棵树:首先先删去树上的一条边,此时树会分裂为两个连通块,然后在两个连通块之间加上一条新的边使 ...
- rownum详解
对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...
- IntelliJ IDEA设置鼠标移动到方法上提示API注释
参考: https://www.cnblogs.com/guazi/p/6474426.html(图片转自此篇文章)
- 记录一次(xheditor-1.1.6-zh-cn.min.js)的错误:Cannot read property 'match' of undefined的问题解决
由于使用了xheditor富文本框,且这个版本是2011年开发的系统,当时只有IE8,所以一切正常. 但是问题来了,今天使用IE11测试和谷歌浏览器测试,发现一直报这个错误: 且google了一下,没 ...
- Scut游戏服务器引擎6.1.5.6发布,直接可运行,支持热更新
1. 增加exe版(console),web版本(IIS)的游戏服宿主程序 2. 增加Model支持脚本化,实现不停服更新 3. 增加Language支持脚本化 4. 修改Sns与Pay Center ...
- Java Web开发(JSP、Servlet)乱码的一揽子解决方案
千万不要看网上那些杂七杂八的解决乱码的文章,解决乱码最好的方法是(没有之一):在所有地方统一采用UTF-8编码. 这其中包括: 1 - 工程 如果使用的是Eclipse,那么打开Preference, ...
- wpa破解学习
TENDA 159031106A iPhone 192.168.0.11 90:27:E4:53:49:D6 18:58:52 PC-201211262044 192.168.0.12 00:F1: ...
- VirtualBox导入XXXX.vdi时报错
virtualbox导入vdi文件时出现以下的问题: 解决方法: windows+R,输入cmd,进入virtualbox的安装文件夹(或者在硬盘中直接进入virtualbox的安装文件夹.在任务栏里 ...
- setOnFocusChangeListener的使用
类似于文本框里面hint文字在初始化的时候显示或者隐藏的操作,就要用到setOnFocusChangeListener的 首先我认为不是太必要- 毕竟当你输入东西时,默认文字自然会消失 当然假设你执意 ...