zoukankan      html  css  js  c++  java
  • HDU1525(Euclid's Game)规律博弈

    Problem Description
    Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Ollie, the second player, does the same with the two resulting numbers, then Stan, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7): 

    25 7
    11 7
    4 7
    4 3
    1 3
    1 0 

    an Stan wins. 

     
    Input
    The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.
     
    Output
    For each line of input, output one line saying either Stan wins or Ollie wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed. 

     
    Sample Input
    34 12 15 24 0 0
     
    Sample Output
    Stan wins Ollie wins
     
    题意:a,b两个数字(假设a是大的那个),a可以减去b的若干倍,完成这一步操作后,数字中有零的人获胜。没有则使用新的a和b继续游戏。
     
    分析:不管怎么采取决策最终都会有(b,a%b)的局面出现,如果a∈[b, 2*b],那么接下来出现的情况只能是[b,a-b];否则有多种情况。
     

    a / b == 1   即a ∈[b , 2b)  , 下一步 只能到(b , a  - b)

    a / b >= 2 时, 先手可以选择让谁来面对 (b ,  a%b)这个局势,  显然先手足够聪明, (b , a % b) 这个局势谁胜谁负 已经清楚, 所以先手必胜

    假设(a%b)/b>=2,导致后手可以选择对自己有利的局势,那先手就可以把当前局势变为(a%b+b,b),让后手被迫选择对自己有利的局势。同理,如果下面好几个局势都是'a'>2*'b'的,第一个面对a>=2*b的局势的聪明人早就想好了怎么走。。

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    int a,b;
    int main()
    {
        while(scanf("%d%d",&a,&b)!=EOF)
        {
            if(a==0&&b==0)
                break;
            if(a<b)
            swap(a,b);
            int flag=0;
            while(b)
            {
                if(a%b==0||a/b>=2)
                break;
                a-=b;
                swap(a,b);
                flag=1-flag;
            }
            if(flag)
                printf("Ollie wins
    ");
            else
                printf("Stan wins
    ");
        }
    }
    View Code
  • 相关阅读:
    SQL常用语句(不定期更新)包含 日期格式,取列
    前端开发 ---浏览器自动刷新
    记录Redis使用中遇到的两个问题(原子性及数据完整性)
    Linux 输出重定向
    逆向手机内核,添加调试支持及绕过反调试
    阿里2014移动安全挑战赛第二题调试笔记
    在redhat6.4上编译z3求解器
    WCF学习笔记一(概述)
    DataReader反射泛型对象
    自定义配置节点(一)
  • 原文地址:https://www.cnblogs.com/switch-waht/p/11498442.html
Copyright © 2011-2022 走看看