SWUST OJ 99:Euclid‘s Game

题目描述

Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move first or second in this game?

According to the above rules, there are two players play this game. Assumptions A write a number on the board at first, then B write it.

Your task is write a program to judge the winner is A or B.

输入

Two unequal positive numbers M and N , M>N (M<1000000)

输出

A or B

样例输入

3 1

样例输出

A

C++代码实现:

解题思路:

只需要求出所给两个数的最大公因子,然后用两个数中最大的数除以这个最大公因子,所得的数就是所有能写的数字的个数。

那么随后要做的就是来判断A和B,谁可以赢(A先写)。当能写的数字个数为奇数的时候,A赢;为偶数时,B赢。就把判断输赢的问题转换成判断所能写数字个数奇偶性的问题。

#include<iostream>
#include<cstdio>
using namespace std;
​
int gcd(int M, int N) {  // 求最大公因数 
    return N ? gcd(N, (M % N)) : M;
}
​
int main() {
    int M, N, num;
    cin>>M>>N;
    num = M / gcd(M, N);
    printf("%c\n", num % 2 ? 'A' : 'B');
    return 0;
} 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值