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

时间:2023-03-09 19:26:48
《Cracking the Coding Interview》——第6章:智力题——题目4

2014-03-20 01:02

题目:无力描述的一道智力题,真是货真价实的智力题,让我充分怀疑自己智力的智力题。有兴趣的还是看书去吧。

解法:能把题目看懂,你就完成80%了,用反证法吧。

代码:

 // 6.4 There is an island with a bunch of people living there.
// The strange thing is, any blue-eyed tourists must leave immediately when they find out they're blue-eyed.
// Everyone can see others' eye colors, but not their own.
// They know there'll be at least one blue-eyed tourists. If no more, the game ends.
// Answer:
// Suppose there're n people and c are blue-eyed.
// If n == 1, that one will soon find out nobody else is blue-eyed, he'll leave on the 1st day.
// If n == 2, those two will see one blue-eyed, they are not sure if c == 1 or c == 2.
// But on the second day, when they still see each other, they know c == 2. Otherwise the one would have left. They'll leave on the 2nd day.
// With mathematical induction, it can be proven that all the c blue-eyed will leave on the cth day together.
// The most difficult part for me, is that when they can't be sure, they will simply stay. I thought every day there must've been someone leaving.
int main()
{
return ;
}