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?

该题类似于下题:

箱子里面有一百个球,甲和乙分别拿球,每次最少一个,最多5个,拿到第一百个球的人获胜。若甲先拿,请问他第一次要拿几个,怎么保证他能拿到第一百个球。 
  思路:反向递推法 
  要拿到第100个球,必须保证拿到第94个球, 
  要保证拿到第94个球,必须保证拿到第88个球, 
  依次类推, 
  每次都要保证拿到第100-6*N个球, 
  最小是100%6=4个球,(100对6取余为4) 
  那么最开始要拿4个球。后来每次确保拿到的个数与乙拿的球的个数和为6.比如,乙拿1个,甲就拿5个;乙拿2个,甲就拿4个,依次类推。 
  总结一下,一般式:如果N个球,甲和乙分别拿球,每次最多拿K个,最少拿一个,甲先拿,要确保甲拿到最后一个球,那么,甲第一次就要拿(N%(K+1))个,后来每次确保与另一方拿的球的个数和为(K+1)个。 
而在本题中,甲第一次要拿N%3 = 0, 1, 2个,只有当N%3 == 0时,无论甲拿1个或2个,结果都是甲输乙赢。而当N%3==1,2时,无论甲第一次拿1个或2个都是甲赢乙输。

//代码
public class Solution {
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int n) {
if(n % 3 == 0) return false;
return true;
}
}

博弈论类型的问题最主要的思想就是逆推

LeetCode Coins in a Line的更多相关文章

  1. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈

    Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, ...

  2. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  3. [LintCode] Coins in a Line II 一条线上的硬币之二

    There are n coins with different value in a line. Two players take turns to take one or two coins fr ...

  4. [LintCode] 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 t ...

  5. Lintcode394 Coins in a Line solution 题解

    [题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...

  6. leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

    参考: LEETCODE 中的member access within null pointer of type 'struct ListNode' 解决 leetcode 编译问题:Line x: ...

  7. Coins in a Line I & II

    Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from ...

  8. Coins in a Line III

    Description There are n coins in a line, and value of i-th coin is values[i]. Two players take turns ...

  9. Coins in a Line

    Description There are n coins in a line. Two players take turns to take one or two coins from right ...

随机推荐

  1. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(五)

    目的: 1. 了解PCI的基本知识,为完成watchdog的设备做准备. 准备知识: 简单的说,PCI 设备分3个空间. 配置空间,IO空间,内存地址空间. PCI设备厂家决定了外设是使用IO空间还是 ...

  2. Python的迭代器(iterator)和生成器(constructor)

    一.迭代器(iterator) 1.迭代器的概述 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器 ...

  3. APUE读书笔记-第14章-高级I/O

    14.1 引言 *高级I/O包括非阻塞I/O.记录锁.系统V流机制.I/O多路转换(select和poll函数).readv和writev函数以及存储映射I/O(mmap) 14.2 非阻塞I/O * ...

  4. JavaScript之模仿块级作用域

    简介:在JavaScript中没有块级作用域的概念.这意味这在块语句中定义的变量,实际上在包含函数中而非语句中创建的.证明代码如下: function outputNumbers(count){ fo ...

  5. JavaScript之arguements对象学习

    简介:在JavaScript中,有一个特殊的对象-Arguements对象,它是当前函数的一个内置属性,它类似与Array对象(数组形式),但不是Array的一个实例.下面通过代码来论证: <s ...

  6. Android - 不管在应用的哪个activity按Home键整个App就结束了

    最开始,客户反映说在用app的时候,来个电话,接完再点app,不是原来的界面,而是重启了.数据都没了,所以就在activity重写onSaveInstanceState方法,将数据保存起来.后经测试发 ...

  7. 转|in、exists、join效率

    EXISTS.IN与JOIN,都可以用来实现形如“查询A表中在(或不在)B表中的记录”的查询逻辑. 在查询的两个表大小相当的情况下,3种查询方式的执行时间通常是:EXISTS <= IN < ...

  8. Objective-C中一些 值得程序员注意的地方(转载)

    1.有关于BOOL陷井方面有如下方面: 关于BOOL条件语句中的比较最好是与NO的值来进行比较,因为BOOL的YES与NO值只是约定,并且编译器将BOOL认作8位二进制数据.若是不小心将一个长于1字节 ...

  9. scala io,ubuntu常见配置

      Ubuntu的scala环境配置 配置scala环境变量同时需要配置java的环境变量 配置方法: 1.先下载linux的相应版本的jdk与scala的二进制压缩包并解压. 解压方法: 先到jdk ...

  10. VC++学习之GDI概述

    VC++学习之GDI概述 图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏 ...