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 }
  • 相关阅读:
    面向连接的网络应用程序--服务器端
    使用完整读写函数的网络应用程序
    套接字编程基础
    网络编程基础
    传输控制协议TCP
    UDP协议
    电子词典
    strtok()函数、fseek()函数、fwrite()函数、fread()函数的使用
    指针与数组
    软件推荐----RDO(Remote Desktop Organizer)
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428861.html
Copyright © 2011-2022 走看看