九度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. ...
随机推荐
- Mysql varchar长度问题
http://dinglin.iteye.com/blog/914276 http://www.cnblogs.com/fakis/archive/2011/03/07/1976532.html ...
- 你值得关注的几种常见的js设计模式
前言 潜水了一段时间,今天空闲时间复盘下之前的知识点,聊聊 js 几种常见的设计模式. 掌握 JavaScript 中常见的一些设计模式,对我们书写规范性代码,可维护性代码有很大的帮助. ps:最近在 ...
- 以root用户身份在jenkins中运行shell命令
以下过程是CentOS 1.打开此脚本(使用VIM或其他编辑器): vim /etc/sysconfig/jenkins 2.找到$JENKINS_USER并更改为“root”: $JENKINS_U ...
- 双网卡环境导致Oracle连接异常
现在就是流行向最高水平看齐,这次项目的部署,好好的SQL Server扔了(有正版授权的企业版,神啊...),逢人就夸:“俺们那上的可是最顶级的Oracle Database System!”.看了看 ...
- 各种优化方法总结比較(sgd/momentum/Nesterov/adagrad/adadelta)
前言 这里讨论的优化问题指的是,给定目标函数f(x),我们须要找到一组參数x.使得f(x)的值最小. 本文下面内容如果读者已经了解机器学习基本知识,和梯度下降的原理. SGD SGD指stochast ...
- Flak快速上手
本文介绍如何上手 Flask . 这里假定你已经安装好了 Flask ,否则请先阅读< 安装>. 如果已安装好Flask,通过以下命令查看 一个简单的例子: from flask impo ...
- apache默认路径
读启动文件 /etc/inid.d/httpd 默认web路径 /var/www/html inux下Apache PHP MYSQL 默认安装路径 apache:如果采用RPM包安装,安装路径应在 ...
- 每天学点Python之bytes
每天学点Python之bytes Python中的字节码用b'xxx'的形式表示.x能够用字符表示,也能够用ASCII编码形式\xnn表示.nn从00-ff(十六进制)共256种字符. 基本操作 以下 ...
- Vue 引入ElementUI 2.0.11:依赖未发现的问题
转自:https://blog.csdn.net/cslucifer/article/details/79019649
- ps快捷键记录
alt+delete 前景色填充 ctrl+delete 背景色填充 alt+shift+鼠标调节 变换选取,做圆环 ctrl+t 自由变换 alt+鼠标拖动 快捷复制某区域 delete ...