LintCode之硬币排成线

image

输入的n可以分为两种情况:

1. 如果n是3的倍数的话,不论A怎么拿B都可以拿(3-A拿的个数)来使其保持是3的倍数,他就一定能拿到最后一块,所以n是3的倍数的话B必胜

2. 如果n不是3的倍数的话,那么A就能够拿1或者2块使其变为3的倍数,就相当于B变成了第一种情况中的A,所以n不是3的倍数的话A必胜

AC代码:

class Solution:
    """
    @param: n: An integer
    @return: A boolean which equals to true if the first player will win
    """
    def firstWillWin(self, n):
        # write your code here
        return True if n % 3 is not 0 else False

参考资料:

1. http://www.lintcode.com/zh-cn/problem/coins-in-a-line/

原文地址:https://www.cnblogs.com/cc11001100/p/7794664.html