[leetcode] Nim Gam
2016-06-23 10:31
351 查看
Nim Gam
这是道有意思的题目:桌子上有一些石头,两个人轮流拿走石头,每人每次只能拿1、2或3块石头,最后拿完石头的人获胜。而且,双方都以最优的方式拿石头。问,当石头数目给定,而且你先拿石头,判断你是否会赢。例如,当有4块石头,你先拿,那么无论你拿1、2或3块,你一定会输。
开始觉得这应该是个回溯问题吧,一会就发现,我把问题想复杂了。。
如果我先手,而且我会赢,那么石头的个数应该满足什么呢?
* 倒数第二次我拿过之前,一定剩4块(如果剩5、6、7块,我一定会输,如果剩8块,我也会赢。)
* 也就是说只要不是4的倍数,我就可以赢喽?
class Solution { public: bool canWinNim(int n) { if(n%4 == 0) return false; return true; } };
这么简单。。
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解