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.

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

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

代码:

 // http://www.careercup.com/question?id=5428361417457664
Answer:
All three baskets are wrongly labeled, thus the "mixed" one is either apple or orange.
If the "mixed" basket is orange, the "apple" basket can't be orange, it must mixed. The "orange" must be apple.
mixed -> orange
orange -> apple
apple -> mixed
If the "mixed" basket is apple, the "orange" basket can't be apple, it must mixed. The "apple" must be orange.
mixed -> apple
orange -> mixed
apple -> orange
05-11 13:00