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. UltraISO制作U盘系统安装盘(图文教程)

    虽然现在的系统盘做的越来越傻瓜化,安装方法也非常多,但是仍然时常有朋友询问怎么安装系统,特别是没有刻录机或不想刻盘,又不懂硬盘安装的朋友,这里特别介绍一种用U盘来安装系统的方法,非量产,量产因U盘芯片 ...

  2. 编程珠玑I算法总结

    主要是根据编程珠玑后面的Algorithm附录总结了一下这本书里面的经典算法. 1 辗转相减求最大公约数 思想:最大公约数能整除i和j,则其一定也能整除i-j(if i>j) int gcd(i ...

  3. logrotate 日志清理后 rsyslog中断问题

    <pre name="code" class="html">随后配置logrotate的配置文件/etc/logrotate.conf,加入下面的内 ...

  4. 雕爷牛腩这样判断零售未来-搜狐IT

    雕爷牛腩这样判断零售未来-搜狐IT 雕爷牛腩这样判断零售未来

  5. 得到client真IP住址

    1.引进的必要性log4j-1.2.14.jar package org.ydd.test; import java.util.Enumeration; import javax.servlet.ht ...

  6. Android 读取Assets中图片

    bgimg0 = getImageFromAssetsFile("Cat_Blink/cat_blink0000.png"); * * 从Assets中读取图片 */ privat ...

  7. HTML之学习笔记(六)添加链接

    html添加链接所用的标签为<a>标签 语法: 定义:从当前页面,跳转到指定页面或文件的一个标签            <a href="URL">热点文字 ...

  8. SQL练习之不反复执行相同的计算

    下面是Demo所需要的代码: CREATE TABLE Fee ( Income ,), overhead ,) ) ,) ,) ,) ,) ,) ,) 现在有一个报表系统,需要根据Fee表获得以下数 ...

  9. ASP.NET MVC开发必看系列

    一.关于HTTP协议的那些事 这可以说我们开发WEB程序的空气,推荐不断温故知新! HTTP协议 (一) HTTP协议详解 HTTP协议 (二) 基本认证 HTTP协议 (三) 压缩 HTTP协议 ( ...

  10. java 中有几种方法可以实现一个线程? 用什么关键字修 饰同步方法? stop()和 suspend()方法为何不推荐使用?

    java5 以前, 有如下两种:第一种:new Thread(){}.start();这表示调用 Thread 子类对象的 run 方法, new Thread(){}表示一个Thread 的匿名子类 ...