zoukankan      html  css  js  c++  java
  • 美图笔试算法题(两个人拿石头判断输赢)

    刚做完美图的笔试,两道编程题,第一道比较简单:找出一串用逗号隔开的字符串中不重复的那个数。

    以下是第二道,时间有限,很多地方没来得及优化,整体逻辑应该没错。

    question:

    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. True for win and false for lose.
    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.

    解:

    public class Main {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            int sum = scanner.nextInt();
            
            //一开始我拿的三种状况
            if (chance(sum,1,1)) {
                System.out.println(true);
            }else if (chance(sum,2,1)) {
                System.out.println(true);
            }else if (chance(sum,3,1)) {
                System.out.println(true);
            }else {
                System.out.println(false);
            }
        }
        
        /**
         * 
         * @param surplus 剩余的石头数量
         * @param take_num 这一次拿走的数量
         * @param people 1代表是我,2代表是朋友
         * @return 返回true,则表示我能赢
         */
        private static boolean chance(int surplus,int take_num,int people) {
            surplus = surplus-take_num;
            
            //当某人拿完,剩余四块时,该人赢
            if (surplus==4) {
                if (people==1) {
                    return true;
                }else {
                    return false;
                }
            }
            
            //当某人拿完,剩余数小于4块时,另一个人赢
            if (surplus<=3) {
                if (people==1) {
                    return false;
                }else {
                    return true;
                }
            }
            
            //此时剩余的石头大于4,继续拿
            
            if (people==1) {//交换人
                people = 2;
                //如果是朋友拿,则必须三个true
                if (chance(surplus,1,people)) {
                    if (chance(surplus,2,people)) {
                        if (chance(surplus,3,people)) {
                            return true;
                        }else {
                            return false;
                        }
                    }else {
                        return false;
                    }
                }else {
                    return false;
                }
            }else {
                people = 1;
                
                //如果是我拿,则只需有一个true即可
                if (chance(surplus,1,people)) {
                    return true;
                }else if (chance(surplus,2,people)) {
                    return true;
                }else if (chance(surplus,3,people)) {
                    return true;
                }else {
                    return false;
                }
            }
        }
    }
  • 相关阅读:
    在winform下实现左右布局多窗口界面的方法(一)
    C# 使用API检查域用户名和密码是否正确
    C#检查网络是否可以连接互联网
    总结:实体类和(XML或二进制)之间相互转(序列化和反序列化)
    XML和实体类之间相互转换(序列化和反序列化)
    C# XML反序列化与序列化举例:XmlSerializer
    XML文件与实体类的互相转换
    Message类的属性Msg所关联的消息ID
    C# Message 消息处理
    在.net中读写config文件的各种方法(自定义config节点)
  • 原文地址:https://www.cnblogs.com/red-code/p/6720114.html
Copyright © 2011-2022 走看看