国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

292. Nim Game (C++)

2019-11-06 06:29:24
字體:
來源:轉載
供稿:網友

題目:

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.

翻譯

你和你的朋友一起玩下面的Nim游戲:在桌子上有一堆石頭,每次你輪流去除1到3塊石頭。 去除最后一塊石頭的人將是贏家。 你會采取第一個回合去除石頭。

你們都很聰明,并且有最好的策略。 編寫一個函數來確定是否可以贏得游戲給定的堆數量的石頭。

例如,如果在堆中有4塊石頭,那么你永遠不會贏得比賽:無論你刪除1,2,或3塊石頭,最后的石頭將永遠被你的朋友刪除。

暗示: 1.如果堆中有5塊石頭,你能找出一種方法去除石頭,讓你永遠是贏家嗎?

思路:

當題目告知4會死的時候就應該想到一定要把對手克制到4,想把對手克制到4,就要先把對手克制到8,,,即每逢4的倍數準會死

解答:

這個答案最簡單的。。

class Solution {public: bool canWinNim(int n) { return n%4!=0; }};相當于:bool canWinNim(int n) { if (n % 4 == 0) return false; else return true;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 慈利县| 万州区| 井陉县| 清镇市| 金溪县| 乐陵市| 敖汉旗| 大化| 乌海市| 明溪县| 陵水| 德清县| 黄冈市| 炎陵县| 马鞍山市| 呼和浩特市| 兴山县| 清苑县| 团风县| 双辽市| 闵行区| 永年县| 富民县| 京山县| 凤翔县| 十堰市| 涞水县| 保德县| 磴口县| 中牟县| 宁波市| 澄迈县| 叶城县| 呼和浩特市| 德兴市| 兴和县| 莲花县| 大城县| 江陵县| 泸水县| 恩施市|