码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode之硬币排成线

时间:2017-11-06 20:11:35      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:images   script   write   layer   html   ntc   png   zh-cn   tab   

技术分享

 

输入的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/

LintCode之硬币排成线

标签:images   script   write   layer   html   ntc   png   zh-cn   tab   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!