国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 嘉定区| 定南县| 高邮市| 玉树县| 灵宝市| 宜川县| 嘉善县| 九龙城区| 沙河市| 额尔古纳市| 富阳市| 调兵山市| 青田县| 嘉荫县| 黎平县| 乌鲁木齐县| 绥阳县| 嫩江县| 资阳市| 林口县| 商河县| 惠水县| 宜丰县| 新津县| 城固县| 绿春县| 栾川县| 宜兴市| 兴安盟| 镇坪县| 巴东县| 泽普县| 三台县| 商丘市| 武邑县| 平潭县| 萝北县| 赞皇县| 景谷| 云林县| 全州县|