• Careercup


    2014-05-11 03:37

    题目链接

    原题:

    You have three jars filled with candies. One jar is filled with banana candies, one jar is filled with lemon candies and one jar has a mix of both. All the jars are mislabelled (i.e. all the jars have wrong labels about what kind of candies they contain). 
    All the candies look very similar in shape, size and color and they even smell the same. The only way to distinguish them is by tasting. 
    You have to eat one and only one candy to determine the correct jar labels. You can eat that one candy from any jar you want as long as you eat only one in total.

    题目:有三罐子糖,其中一罐香蕉糖、一罐柠檬糖、一罐是两者混合。每个管子上贴的标签都是错的。如果允许你选一罐拿出一颗糖吃掉,能否辨认出所有罐子实际装了什么糖?

    解法:只能吃一颗糖,根据对称性原则,吃香蕉或者柠檬是等效的,所以肯定吃混合的那一罐。因为标签全是错的,所以吃出来是什么糖,那一罐就是什么糖。接下来顺着推出其他两罐糖就可以了。排除法是个好东西。

    代码:

     1 // http://www.careercup.com/question?id=5428361417457664
     2 Answer:
     3     All three baskets are wrongly labeled, thus the "mixed" one is either apple or orange.
     4     If the "mixed" basket is orange, the "apple" basket can't be orange, it must mixed. The "orange" must be apple.
     5         mixed -> orange
     6         orange -> apple
     7         apple -> mixed
     8     If the "mixed" basket is apple, the "orange" basket can't be apple, it must mixed. The "apple" must be orange.
     9         mixed -> apple
    10         orange -> mixed
    11         apple -> orange
    12         
  • 相关阅读:
    PlantUML —— 应用于 Eclipse 的简单快速的 UML 编辑软件
    PlantUML类图
    Java 基于log4j的日志工具类
    RabbitMQ Hello world(二)
    redis lua 初体验
    mysql 批处理 innodb_flush_at_trx_commit 测试分析
    mysql 服务日志 5.7.29
    redis 浅谈事务
    redis list 基本操作
    redis hash 基本操作
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3721319.html
Copyright © 2020-2023  润新知