zoukankan      html  css  js  c++  java
  • 【POJ】2348 Euclid's Game(扩欧)

    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
    ------------------------------------------------------------------
    题意:两个人在玩一个类似gcd的游戏,从两个数中不断用较大数中减去较小数的倍数,谁先得到0谁获胜,问谁赢。
    分析:一开始还以为是要跑gcd,然而还是太naive了。看了一下《挑战》,总结了一下思路。
      {策略1}:对于(a,b)(a>b)来说,如果a是b的倍数,那么肯定当前者赢。
      {策略2}:对于(a,b)(a>b)来说,如果a<2*b,那么意味着下一个状态必定是(b,a-b),因为此时a不可能减去更多倍的b,以此类推之后,永远不会出现a%b==0的情况,因为如果能出现的话,策略1就会将其判定了。那么肯定当前者赢。
      用上策略1和策略2就AC了。
     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 int main()
     5 {
     6     long long a,b;//f=1表示stan赢 
     7     while( scanf("%lld%lld",&a,&b)==2 && a!=0 && b!=0 )
     8     {
     9         int f=1;
    10         while(1)
    11         {
    12             if(a>b) swap(a,b);
    13             if(b%a==0) break;//策略1
    14             if(b-a>a) break;//策略2
    15             b-=a;
    16             f*=-1; 
    17         }
    18         if(f==1) printf("Stan wins
    ");
    19         else printf("Ollie wins
    ");
    20     }
    21     return 0;
    22 }
  • 相关阅读:
    iOS 怎么在一个函数执行完毕得到某个参数值后再去执行他下边的代码
    微信小程序开发(三)-----手动创建目录结构
    微信小程序开发(二)-----项目的创建
    微信小程序开发(一)-----工具的安装
    Block传值讲解与使用
    Mybatis xml约束文件的使用
    Spark Streaming 整合 Kafka
    Scala 大数据 常用算法收集
    WPF自定义动画控件 风机
    wpf 错误 执行了 QueryInterface 调用,请求提供 COM 可见的托管类“BoilerMonitoringV1._0.MapControl”的默认 IDispatch 接口。
  • 原文地址:https://www.cnblogs.com/noblex/p/7577488.html
Copyright © 2011-2022 走看看