《Cracking the Coding Interview》——第6章:智力题——题目3
2014-03-20 00:48
题目:有3升的瓶子和5升的瓶子,只允许倒满、倒到满为止、或是泼光三种操作,怎么搞出4升水呢?
解法:如果A和B是互质的两个正整数,且A<B,令X=B-A,则(X%B, 2X%B, 3X%B, ..., BX%B)正好就是0~n-1的一个排列。也就是说,两瓶子的容积如果互质,就可以配出0~B之间的任意容积。至于怎么配,请看代码。
代码:
// 6.3 Given two jugs with size x and y, and a volume v between 0 and y inclusive. Show how you're gonna get that value.
// Note that you can only fill a jug, empty a jug or pour from one into another.
// x and y will be positive and relatively prime.
#include <cstdio>
using namespace std; int gcd(int x, int y)
{
return x == ? y : gcd(y % x, x);
} int main()
{
int x, y;
int vx, vy;
int v; while (scanf("%d%d%d", &x, &y, &v) == && (x > && y > && v > )) {
if (x > y) {
continue;
}
if (gcd(x, y) > ) {
continue;
} vx = ;
vy = y;
printf("[%d, %d]: fill y\n", vx, vy);
while (vy != v) {
if (vy < x) {
vx = vy;
vy = ;
printf("[%d, %d]: pour y into x\n", vx, vy);
vy = y;
printf("[%d, %d]: fill y\n", vx, vy);
vy = vy - (x - vx);
vx = x;
printf("[%d, %d]: pour y into x\n", vx, vy);
vx = ;
printf("[%d, %d]: empty x\n", vx, vy);
} else {
vy -= x;
vx = x;
printf("[%d, %d]: pour y into x\n", vx, vy);
vx = ;
printf("[%d, %d]: empty x\n", vx, vy);
}
}
} return ;
}
《Cracking the Coding Interview》——第6章:智力题——题目3的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第6章:智力题——题目4
2014-03-20 01:02 题目:无力描述的一道智力题,真是货真价实的智力题,让我充分怀疑自己智力的智力题.有兴趣的还是看书去吧. 解法:能把题目看懂,你就完成80%了,用反证法吧. 代码: / ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目6
2014-03-20 01:14 题目:有100栈灯,一开始都关着.如果你按照n从1~100的顺序,每次都掰一下n的倍数的开关(开->关,关->开),那么到最后有多少灯是亮的? 解法:这个 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目5
2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...
随机推荐
- nodejs封装的webget webpost方法
在我之前的项目中,经常用到Nodejs通过post\get方法访问其它网站.webapi.下面是我封装的 Get.Post方法,很适合在一些web字符串收发场景使用(暂不支持文件.二进制流等传输). ...
- MySQL数据库实验二:单表查询
实验二 单表查询 一.实验目的 理解SELECT语句的操作和基本使用方法. 二.实验环境 是MS SQL SERVER 2005的中文客户端. 三.实验示例 1.查询全体学生的姓名.学号.所在系. ...
- 119. Pascal's Triangle II (Amazon) from leetcode
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- 2017.11.17 C++系列---用malloc动态给c++二维数组的申请与释放操作
方法一:利用二级指针申请一个二维数组. #include<stdio.h> #include<stdlib.h> int main() { int **a; //用二级指针动态 ...
- asp .net core 中间件的简单 使用
在startup 的cs文件中 2.捕获异常的中间件 可以在浏览器中 显示异常信息 在开发环境的境况下,该中间件会帮你 捕获异常
- python中的for循环如何控制步长
for i in range(开始/左边界, 结束/右边界, 步长): print i 例如 for i in range(1, 10, 2): print i 等价于 for (i=1;i<= ...
- 在VS中使用Boost库出现Macro redefinition错误的解决方法(warning C4005)
最近使用Boost库做多线程开发,可视在vs中编译工程师总是遇到Macro redefinition错误,类似下面的错误描述 1>c:\program files (x86)\microsoft ...
- django模板层(templates)
1.基础部分 通过使用模板,就可以在URL中直接调用HTML,它还是松耦合的,灵活性强,而且更容易维护 而且可以将变量通过一定的方式嵌入到HTML中,最终渲染到页面,总的来说基于模板完成了数据与用户之 ...
- java mysql多次事务 模拟依据汇率转账,并存储转账信息 分层完成 dao层 service 层 client层 连接池使用C3p0 写入库使用DBUtils
Jar包使用,及层的划分 c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3 ...
- 关于 Angular引用Material出现node_modules/@angular/material/button-toggle/typings/button-toggle.d.ts(154,104): error TS2315: Type 'ElementRef' is not generic.问题
百度了好久 ,,,最后谷歌出来了.. 该错误可能来自于您将@ angular / material设置为6.0.0, 但所有其他Angular包都是5.x.您应该始终确保Material主要版本与An ...