《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,怎么扔才能减少最坏情况下的扔的次数? 解 ...
随机推荐
- Multi-modal Sentence Summarization with Modality Attention and Image Filtering 论文笔记
文章已同步更新在https://ldzhangyx.github.io/,欢迎访问评论. 五个月没写博客了,不熟悉我的人大概以为我挂了…… 总之呢这段时间还是成长了很多,在加拿大实习的两个多月来 ...
- Simotion应用案例,使用Simotion web server调试,使用Project Generator创建项目,Simosim模拟运行运行项目
Simotion web server simotion项目设计和调试过程中,web server功能越来越常用.例如Project generator生成的FBAxis, winder, print ...
- 查看Linux网卡地址,网络地址
查看网络地址 ip a 或ip addr show 或ifconfig,此指令在部分linux系统中不支持
- oracle 创建SDO_Geometry表
Oracle Spatial由一坨的对象数据类型,类型方法,操作子,函数与过程组合而成.一个地理对象作为一个SDO_GEOMETRY对象保存在表的一个字段里.空间索引则由普通的DDL和DML语句来建立 ...
- B. Qualifying Contest_排序
B. Qualifying Contest time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Flash Media Live Encoder参数表
Flash Media Live Encoder命令行推流Flash Media Live Encoder NotesFlash Media Live Encoder 除了直接以 GUI 方式操作之外 ...
- java基础语法:非法修饰符组合 abstract
abstract 与 final :abstract 是需要被继承以实现的,final却说你不能被修改,逻辑错误 abstract 与 private:同样的abstract 需要被子类实现,但pr ...
- call()和apply()
每个函数都包含apply和call方法. 相同点:都接收两个参数,一个是在其中运行函数的作用域,另一个是参数: 不同点:call方法和apply的不同之处在于接收参数的方式不同: apply方法第二个 ...
- WPF与Silverlight对比
1.WPF中控件的肤色可以直接:telerik:StyleManager.Theme=”XXXXX”,不用再导入肤色的dll包.可Silverlight使用系统肤色时,要导入肤色的dll包. WPF引 ...
- jQuery、Angluar、Avalon对比
最近在慕课网看一些关于avalon的视频,记录下一些笔记及代码实例以便日后自己复习可以用到,另外也可以给不想花时间看视频的小伙伴提供一丝丝帮助 这里主要是做一个简单的todolist 分别用三种不同的 ...