zoukankan      html  css  js  c++  java
  • 292. Nim Game

    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.

    此题比较有趣,举例子就可以看的出来了,只要是4的倍数,就会赢的,代码如下:

    1 public class Solution {
    2     public boolean canWinNim(int n) {
    3         if(n%4==0) return false;
    4         else return true;
    5     }
    6 }
  • 相关阅读:
    php——验证身份证是否合法的函数
    php——离线执行任务
    代码整洁之道
    js自适应屏幕高度
    SSH Junit4测试
    Java Persistence with Hibernate
    SSH搭建
    js整理
    Hibernate 应用
    对学习的一点感想
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6482027.html
Copyright © 2011-2022 走看看