zoukankan      html  css  js  c++  java
  • LeetCode 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.

    Hint:

    1. If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
     1 public class Solution {
     2     public boolean canWinNim(int n) {
     3         if(n%4 == 0)
     4         {
     5             return false;
     6         }
     7         else
     8         {
     9             return true;
    10         }
    11     }
    12 }
     1 /*************************************************************************
     2     > File Name: LeetCode292.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Tue 10 May 2016 07:30:06 PM CST
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     Nim Game
    11     
    12     You are playing the following Nim Game with your friend: 
    13     There is a heap of stones on the table, 
    14     each time one of you take turns to remove 1 to 3 stones. 
    15     The one who removes the last stone will be the winner. 
    16     You will take the first turn to remove the stones.
    17 
    18     Both of you are very clever and have optimal strategies for the game. 
    19     Write a function to determine whether you can win the game 
    20     given the number of stones in the heap.
    21 
    22     For example, if there are 4 stones in the heap, 
    23     then you will never win the game: 
    24     no matter 1, 2, or 3 stones you remove, 
    25     the last stone will always be removed by your friend.
    26 
    27     Hint:
    28 
    29     If there are 5 stones in the heap, 
    30     could you figure out a way to remove the stones 
    31     such that you will always be the winner?
    32 
    33  ************************************************************************/
    34  
    35 #include <stdio.h>
    36 
    37 int canWinNim( int n )
    38 {
    39     int ret = 0;
    40     if( n%4 != 0 )
    41     {
    42         ret = 1;
    43     }
    44     return ret;
    45 }
    46 
    47 int main()
    48 {
    49     int n = 5;
    50     int ret = canWinNim(n);
    51     printf("%d
    ", ret);
    52     return 0;
    53 }
  • 相关阅读:
    (原)Lazarus 异构平台下多层架构思路、DataSet转换核心代码
    (学)新版动态表单研发,阶段成果3
    (学) 如何将 Oracle 序列 重置 清零 How to reset an Oracle sequence
    (学)XtraReport WebService Print 报错
    (原)三星 i6410 刷机 短信 无法 保存 解决 办法
    (原) Devexpress 汉化包 制作工具、测试程序
    linux下网络配置
    apache自带ab.exe小工具使用小结
    Yii::app()用法小结
    PDO使用小结
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428861.html
Copyright © 2011-2022 走看看