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 }
  • 相关阅读:
    45_ansible概述、ansible基础 、ad-hoc、批量配置管理
    44_自定义镜像及仓库、持久化存储 、 Docker网络架构
    43_Docker概述、部署Docker、Docker镜像、Docker基本命令
    42_KVM简介、 Virsh管理 、 自定义虚拟机、虚拟设备管理
    41_iptables防火墙 filter表控制 扩展匹配 nat表典型应用
    40_系统审计 服务安全 Linux安全之打补丁
    39_加密与解密 AIDE入侵检测系统 扫描与抓包
    38_Linux基本防护 用户切换与提权 SSH访问控制 SELinux安全 、SSH访问控制 SELinux安全
    hdu5530
    bzoj3456
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428861.html
Copyright © 2011-2022 走看看