Coins in a Line

There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.

Could you please decide the first play will win or lose?

http://www.lintcode.com/en/problem/coins-in-a-line/

Discussion

在一个综艺节目中看到了这个游戏,两个人数数,每次可以数1个或者2个,谁先数到30谁输,这个题目说谁拿到最后一个谁win。

写几个就看出来规律了TTFTTFTTFTTF........

bool firstWillWin(int n) {
        if(n <= 0) return false;
        return n%3==1 || n%3==2;
    }

如果是谁拿最后一个谁输呢?

bool firstWillWin(int n) {
        if(n <= 0) return false;
        return n%3==0 || n%3==2;
    }

回到综艺节目那个题,第一个说的总是能赢的,因为30%3==0,怎么赢呢? 只要不说3的倍数就可以了。

results matching ""

    No results matching ""