LintCode 394: First Will Win
LintCode 394: First Will Win
题目描述
有n个硬币排成一条线。两个参赛者轮流从右边依次拿走1或2个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。
请判定 第一个玩家 是输还是赢?
样例
n = 1, 返回 true.
n = 2, 返回 true.
n = 3, 返回 false.
n = 4, 返回 true.
n = 5, 返回 true.
Sat Feb 26 2017
思路
一般这种博弈的题目都是假设双方足够聪明,能够用最优的策略取得胜利的,看似很麻烦,实际上却简化了问题,因为这样的话每一个状态都确定了。
假设硬币数n是三的倍数,那么不管第一个人拿第一次拿了一个硬币还是两个硬币,第二个人都会跟第一个人拿的数量凑够三个,如此循环,最终肯定是第二个人获胜。
假设硬币数n不是3的倍数,则不管3除n是余1还是余2,第一个人第一次只需要拿走余数个硬币,剩下的硬币数就是3的倍数了,情况就变成了上一种情况,只不过胜负易位了。
代码
// 硬币排成线
bool firstWillWin(int n)
{
return n % 3 != 0;
}
LintCode 394: First Will Win的更多相关文章
- LintCode 395: First Will Win 2
LintCode 395: First Will Win 2 题目描述 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 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个石子之后都是必胜,则当前必败 ...
- [LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
- [LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈
Description There are n coins with different value in a line. Two players take turns to take one or ...
- Linux常用网络命令整理
Linux上有一些非常常用的命令,来帮助我们监控网络状况. 1.Tcpdump命令 tcpdump可以将网络中传送的数据包的"头"完全截获下来提供分析.它支持针对网络层.协议.主机 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- [LeetCode] questions conclusion_ Dynamic Programming
Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
随机推荐
- HashMap和HashTable源码分析
HashMap HashMap是一个实现了Map接口的Hash表.提供所有Map的操作,并且允许null key和null value.HashMap几乎等同于HashTable,只不过HashMap ...
- 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 微信Web开发者工具-下载、安装和使用图解
开发和测试小程序,需要借助微信官方提供的微信Web开发者工具进行预览和调试代码,从下载安装到使用,大致的流程如下: 1.下载安装包 下载地址传送门:https://developers.weixin. ...
- 【C】多线程编程笔记
1. pthread_create(pthread类型指针变量 ,NULL ,函数 ,函数参数[多个参数用结构体传]) 2. pthread_join(pthread类型指针变量, 返回一般为null ...
- 【bzoj2600】[Ioi2011]ricehub 双指针法
题目描述 给出数轴上坐标从小到大的 $R$ 个点,坐标范围在 $1\sim L$ 之间.选出一段连续的点,满足:存在一个点,使得所有选出的点到其距离和不超过 $B$ .求最多能够选出多少点. $R\l ...
- Testng 运行Cannot find class in classpath
用Testng运行多个class,结果报: org.testng.TestNGException: Cannot find class in classpath: Salesman at or ...
- BZOJ3712 PA2014Fiolki(kruskal重构树)
对合并过程建树.然后只需要按照时间顺序考虑每个反应就行了,时间顺序根据lca的深度确定. #include<iostream> #include<cstdio> #includ ...
- 题解 P5015 【标题统计】
既然这个题这么水 大家不如来盘点一下算法呗 首先说一个事:逗号表达式 这玩意的值是最后一个表达式的值 那么我们就可以愉快的放进循环条件里摩擦 话说这个应该是基础吧,大多数代码都可以这样干 具体可以后面 ...
- Bond UVA - 11354(并查集按秩合并)
题意: 给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路. 解析: 应该都想过用prime的次小生成树做..但二维数组开不了那么大..所以只能用kruskal了.... #incl ...
- [您有新的未分配科技点] 无旋treap:从单点到区间(例题 BZOJ1500&NOI2005 维护数列 )
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MB Description Input 输入的第1 行包含两个数N 和M(M ≤20 ...