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 }
  • 相关阅读:
    javaApi Swagger配置
    java跨域配置
    applation.properties与applation.yml关于sql数据库连接配置的区别
    SpringBoot学习记录一
    Centos命令行报bash:.....:command not found的解决办法
    Referenced file contains errors (http://JAVA.sun.com/xml/ns/j2ee/web-app_2_5.xsd).
    C# 两种封装的区别
    此 ObjectContext 实例已释放,不可再用于需要连接的操作。
    .net MVC ajax传递数组
    正则表达式移除首部尾部多余字符
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428861.html
Copyright © 2011-2022 走看看