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 }
  • 相关阅读:
    Cognos无法解密来着内容库的用户名和密码凭证
    JavaScript 中的对象引用
    npm install出现的错误
    箭头函数中this的用法
    [译]ArcGIS Server Map Service Cache的组织结构
    [C#] 如何选择抽象基类与接口
    数据库设计规范
    在DataTable中查询应该注意的问题
    坦克大战总结
    仓库管理系统总结(1)
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428861.html
Copyright © 2011-2022 走看看