zoukankan      html  css  js  c++  java
  • VijosP1208欧几里德的游戏 博弈

    欧几里德的游戏

     

    题目描述

    欧 几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的。给定两个正整数M和N,从Stan开始,从其中较大的一 个数,减去较小的数的正整数倍,当然,得到的数不能小于0。然后是Ollie,对刚才得到的数,和M,N中较小的那个数,再进行同样的操作……直到一个人 得到了0,他就取得了胜利。下面是他们用(25,7)两个数游戏的过程:
        Start:25 7
        Stan:11 7
        Ollie:4 7
        Stan:4 3
        Ollie:1 3
        Stan:1 0
        Stan赢得了游戏的胜利。
    现在,假设他们完美地操作,谁会取得胜利呢?

    输入

    第一行为测试数据的组数C。下面有C行,每行为一组数据,包含两个正整数M, N。(M, N不超过长

    输出

    对每组输入数据输出一行,如果Stan胜利,则输出“Stan wins”;否则输出“Ollie wins”

    样例输入

    2
    25 7 
    24 15
    

    样例输出

    Stan wins
    Ollie wins

    假设博弈现在从Stan开始
    (1)如果 n % m == 0 先手stan 可以直接获胜

    (2)如果 n / m > 1 && n%m != 0, 先手stan 可以使 n = n%m + m > m, Ollie只可以取 n%m , 剩下 (m , m), 故 stan 赢
     (3)那么其他情况, 轮到对手的选择权, 如此反复直到达到(1)、(2)结束
     1 #include <iostream>
     2 #include <sstream>
     3 #include <fstream>
     4 #include <string>
     5 #include <vector>
     6 #include <deque>
     7 #include <queue>
     8 #include <stack>
     9 #include <set>
    10 #include <map>
    11 #include <algorithm>
    12 #include <functional>
    13 #include <utility>
    14 #include <bitset>
    15 #include <cmath>
    16 #include <cstdlib>
    17 #include <ctime>
    18 #include <cstdio>
    19 #include <cstring>
    20 
    21 using namespace std;
    22 
    23 
    24 typedef long long int ll;
    25 typedef pair<int, int> P;
    26 int read() {
    27     int x=0,f=1;
    28     char ch=getchar();
    29     while(ch<'0'||ch>'9') {
    30         if(ch=='-')f=-1;
    31         ch=getchar();
    32     }
    33     while(ch>='0'&&ch<='9') {
    34         x=x*10+ch-'0';
    35         ch=getchar();
    36     }
    37     return x*f;
    38 }
    39 const double pi=3.14159265358979323846264338327950288L;
    40 const double eps=1e-10;
    41 const int mod = 1e9 + 7;
    42 const int INF = 0x3f3f3f3f;
    43 const int INT = 0x7fffffff;
    44 const int MAXN = 433;
    45 const int xi[] = {0, 0, 1, -1};
    46 const int yi[] = {1, -1, 0, 0};
    47 
    48 
    49 int main() {
    50     int n, m, t;
    51     scanf("%d", &t);
    52     while(t--){
    53         scanf("%d%d", &n, &m);
    54         if(n < m) swap(n, m);
    55         int Stan = 1;
    56         while(n/m == 1 && n%m){
    57             int t = n%m;
    58             n = m;
    59             m = t;
    60             Stan = -Stan;
    61         }
    62         if(Stan == 1) printf("Stan wins
    ");
    63         else printf("Ollie wins
    ");
    64     }
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    C#多线程(16):手把手教你撸一个工作流
    C#多线程(15):任务基础③
    C#多线程(14):任务基础②
    C#多线程(13):任务基础①
    C#多线程(12):线程池
    C#多线程(11):线程等待
    C#多线程(10):读写锁
    C#多线程(9):多阶段并行线程
    C#多线程(8):线程完成数
    C#多线程(7):手动线程通知
  • 原文地址:https://www.cnblogs.com/cshg/p/5779633.html
Copyright © 2011-2022 走看看