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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[LeetCode] NO.292 Nim Game

發(fā)布時(shí)間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [LeetCode] NO.292 Nim Game 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

[題目]?

? ? ? ? You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. 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.

? ? ? ? For example, 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.

[題目解析]: 根據(jù)題目進(jìn)行分析,當(dāng)有4個(gè)石頭的時(shí)候,無(wú)論你拿1,2還是3個(gè),對(duì)手都可以最后一個(gè)拿完,你永遠(yuǎn)不會(huì)贏。

? ? ? ? 依次類(lèi)推,當(dāng)有5個(gè)石頭的時(shí)候,你拿1個(gè),留給對(duì)方4個(gè),對(duì)方必輸;當(dāng)有6個(gè)石頭的時(shí)候,你拿2個(gè),對(duì)方必輸;當(dāng)有7個(gè)石頭的時(shí)候,你拿3個(gè),對(duì)方必輸。而當(dāng)有8個(gè)石頭的時(shí)候,你無(wú)論拿1,2,還是3個(gè),都留給對(duì)方7,6,5三種,對(duì)方都必贏。繼續(xù)類(lèi)推呢,發(fā)現(xiàn)規(guī)律了嗎?n%4等于0的時(shí)候,你必輸,其他情況你都可以贏。

? ? ? ??

public boolean canWinNim(int n) {if(n <= 0) return false;return !(n%4 == 0);}

?

轉(zhuǎn)載于:https://www.cnblogs.com/zzchit/p/5767704.html

總結(jié)

以上是生活随笔為你收集整理的[LeetCode] NO.292 Nim Game的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。