日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 292. Nim Game

發布時間:2024/5/6 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 292. Nim Game 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

292. Nim Game 尼姆游戲

You are playing the following Nim Game with your friend:
您正在和您的朋友玩以下NIM游戲:
There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones.
桌上有一堆石頭,你們中的每一個人輪流移動1到3塊石頭。
The one who removes the last stone will be the winner.
移走最后一塊石頭的人將是獲勝者。
You will take the first turn to remove the stones.
你將在第一個回合移動石塊。

Both of you are very clever and have optimal strategies for the game.
你們兩個都很聰明,都有最佳的游戲策略。
Write a function to determine whether you can win the game given the number of stones in the heap.
寫一個函數來決定你是否能在給定石頭堆數的情況下贏得比賽。

Example 例子

Input: 4
Output: false
Explanation: If there are 4 stones in the heap, then you will never win the game;No matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
說明:如果堆中有4塊石頭,那么你將永遠無法贏得比賽;無論你移除1塊、2塊或3塊石頭,最后一塊石頭都將被你的朋友移除。

思路

n=1,2,3時你贏,n=4時對手贏;

n=5時,根據上一點,你只需先拿走1個石子,于是剩下4個,由于“n=4時對手贏”,而對手的對手是你,因此你必贏;

n=6,7時同理;

n=8時,你有3種選擇,結果分別剩下7,6,5個石子,此時對手站在你的角度,根據上面結論,對手必贏。

……如何循環下去,你總是3贏1輸。

code

class Solution { public:bool canWinNim(int n) {return n%4;} };

總結

以上是生活随笔為你收集整理的LeetCode 292. Nim Game的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。