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 }
  • 相关阅读:
    JavaScript脚本的两种放置方式
    对象 属性 事件 方法
    媒体查询
    HTML5布局
    图像
    布局
    列表,表格,表单
    盒子
    vue.js常见面试题及常见命令介绍
    Winform读报工具
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6482027.html
Copyright © 2011-2022 走看看