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.
【题目分析】
博弈论。
【代码】
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a,b,win=1;
int main()
{
while (scanf("%d%d",&a,&b)!=EOF&&a&&b)
{
win=1;
if (a<b) swap(a,b);
while (a)
{
if (a/2>=b||a==b) break;
a-=b;
int x=a;
a=b;
b=x;
win^=1;
}
if (win) printf("Stan wins\n");
else printf("Ollie wins\n");
}
}
本文介绍了一种基于两个自然数的博弈论游戏算法。玩家通过交替操作减小数值,直至一方获胜。文章提供了详细的实现代码,并通过实例展示了游戏过程。
1034

被折叠的 条评论
为什么被折叠?



