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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

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

  7. 《Cracking the Coding Interview》——第6章:智力题——题目4

    2014-03-20 01:02 题目:无力描述的一道智力题,真是货真价实的智力题,让我充分怀疑自己智力的智力题.有兴趣的还是看书去吧. 解法:能把题目看懂,你就完成80%了,用反证法吧. 代码: / ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. 《Cracking the Coding Interview》——第6章:智力题——题目6

    2014-03-20 01:14 题目:有100栈灯,一开始都关着.如果你按照n从1~100的顺序,每次都掰一下n的倍数的开关(开->关,关->开),那么到最后有多少灯是亮的? 解法:这个 ...

  10. 《Cracking the Coding Interview》——第6章:智力题——题目5

    2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...

随机推荐

  1. April 3 2017 Week 14 Monday

    Don't worry about finding your soul mate. Find yourself. 欲寻佳侣,先觅本心. You may fail to find your soul m ...

  2. Bean的初始化和销毁

    在我们实际开发的时候,经常会遇到在Bean在使用之前或者之后做些必要的操作,Spring对Bean的生命周期的操作提供了支持.在使用Java配置和注解配置下提供如下两种方式.    1.Java配置方 ...

  3. 【P1330】 封锁阳光大学

    两个和谐河蟹不能在同一条边的两端.所以对于每条边.只有一个节点有和谐河蟹 所以说,我们可以将有和谐河蟹的看做一种颜色,或则是状态.没有河蟹看做另一种言颜色 这样边变成了二分图染色 所以嗯~(・∀・) ...

  4. 面试-Spring理解

    转自http://hui.sohu.com/infonews/article/6331404387079946240 spring呢,是pivotal公司维护的一系列开源工具的总称,最为人所知的是sp ...

  5. ElasticSearch 集群原理

    节点 一个运行中的EasticSearch 被称为一个节点,而集群是由多个用于拥有相同cluster.name配置的节点组成,它们共同承担数据和负载的压力,当有新的节点加入或移除,集群会重新平均分布所 ...

  6. Java OOP——第五章 异常

    1. 尝试通过if-else来解决异常问题: Eg: public class Test2 {       public static void main(String[] args) {       ...

  7. scrapy--boss直聘

    Hi,大家好.有段时间没来更新scrapy爬取实例信息了,前2天同事说爬取拉勾,boss直聘等网站信息比较困难.昨天下午开始着手爬取boss直聘内Python爬虫的信息,比想象中的简单很多. 需要解决 ...

  8. dts--tests(一)

    cmdline.py """ DPDK Test suite. Test cmdline. """ import utils from te ...

  9. manjaro无法使用ifconfig查ip

    manjaro中自带的查看网络的命令是: ip addr 可以了解一下ip命令都有哪些功能 如果还是想要 ifconfig 需要安装net-tools 安装命令: sudo pacman -S net ...

  10. python_字符串_常用处理

    1. 输出原序列的反向互补序列 in1 = open("brca1.fasta", "r") out1 = open("re_brca1.fasta& ...