POJ 2348 (博弈)
2016-03-07 14:49
621 查看
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. 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 Source Waterloo local 2002.09.28 |
对于当前状态1,不管过程如何两个整数(n, m)(n>=m)都将会变成状态2:(m, n%m),所以如果状态2必败,那么状态1就必胜;否则的话考虑n最多减去几次m,如果n/m的结果取整大于1,那么先手就可以使状态变成(n%m+m, m)从而必胜,否则必败。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool win (int n, int m) {
if (m == 0)
return 0;
if (!win (m, n%m))
return 1;
int t = n/m;
if (t > 1)
return 1;
return 0;
}
int main () {
int n, m;
while (scanf ("%d%d", &n, &m) == 2) {
if (!n && !m)
break;
if (n < m)
swap (n ,m);
printf ("%s\n", win (n, m) ? "Stan wins" : "Ollie wins");
}
return 0;
}
相关文章推荐
- linux下根据进程名定时杀死/重启进程
- Objective-C适用C数学函数 <math.h>
- JSON数据的格式和解析(使用GSON)入门
- pitch yaw roll是什么
- Mac apache简单开关和IP禁用
- VS2010打开VS2013的项目-Web应用程序项目XX已配置为使用IIS
- S2 第三章SQL编程
- FPGA功耗那些事儿(转载)
- 基础练习 十六进制转八进制
- js数组循环遍历数组内所有元素的方法
- Easyui中使用jquery或js动态添加元素时出现的样式失效的解决方法
- [LeetCode]96. Unique Binary Search Trees
- HTML5通信API
- Windows下PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。
- Android Studio 不得不知的20大快捷键
- Objective-C中的meta-class
- 可伸缩性, 可用性和稳定性模式 Scalability, Availability & Stability Patterns
- java中的继承
- android 设计模式
- swift:类class是浅拷贝 结构体struct是深拷贝(代码理解)