zoukankan      html  css  js  c++  java
  • HDU1525 Euclid's Game

    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. 

    InputThe input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Stan always starts.OutputFor 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,较大数减去较小数的任意倍数,结果不能小于0,将两个数任意一个数减到0的为胜者。

    错误思路:

    最开始我是这样想的:(a,b)->(b,a%b),算一个转移,中间可以减a/b次;(b,a%b)->(a%b,b%(a%b))需要..次。把每一个转移看成一个堆,那么每堆可以任选,就成了Nim博弈了。代码如下:

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    int main()
    {
        int Xor,a,b;
        while(~scanf("%d%d",&a,&b)){
            if(a==0&&b==0) return 0;
            Xor=0;
            while(true){
                if(a==0||b==0) break;
                if(a<b) swap(a,b);
                Xor=Xor^(a/b);
                a=a%b;
            }
            if(Xor) printf("Stan wins
    ");
            else printf("Ollie wins
    ");
        } return 0;
    }
    View Code

    但是这样做是有问题的,因为Nim博弈是任选一堆的任意个,而这样转化的从前往后的堆里选任意个。所以不一样。

    正确打开方式:   假设a大于b a == b.  N态 a%b == 0. N态 

                                 a >= 2*b,先手能决定谁取(b,a%b),并且知道(b,a%b)是P态还是N态.    N态

                                 b<a<2*b, 只能 -->(b,a-b) , 然后再进行前面的判断.

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    int main()
    {
        int num,a,b;
        while(~scanf("%d%d",&a,&b)){
            if(a==0&&b==0) return 0;
            num=1;
            while(true){
                if(a<b) swap(a,b);
                if(a%b==0||b==0) break;
                if(a>=2*b) break;
                a=a%b;
                num++;
            }
            if(num&1) printf("Stan wins
    ");
            else printf("Ollie wins
    ");
        } return 0;
    }
  • 相关阅读:
    浅析嵌入式程序设计中的优化问题
    TCP粘包问题
    使用python 批量 配对t检验 医学 基础研究 数据分析
    Ubuntu误删系统文件修复办法
    飞思卡尔powerpc交叉编译环境的
    ubuntu packege下载网址
    数组对象里面对日期进行排序
    c# 字符串以逗号分割属性加上单引号
    Vue mysql 变量赋值, 获取数组
    Element vue Select 下拉框默认
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8404655.html
Copyright © 2011-2022 走看看