一、题目描述

Two players, Singa and Suny, play, starting with two natural numbers. Singa, 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 Suny, the second player, does the same with the two resulting numbers, then Singa, 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 Singa wins.

二、输入

The input consists of a number of lines. Each line contains two positive integers (<2^31) giving the starting two numbers of the game. Singa always starts first. The input ends with two zeros.

三、输出

For each line of input, output one line saying either Singa wins or Suny wins assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.

四、解题思路

一开始,看完题目完全没头绪,不知道从何下手。后来想想,这是一个博弈游戏,按照游戏规则,如果其中一个数不比另外一个数大一倍或一倍以上时,游戏将进行简单地相减。

if(nultipleNum2 > nultipleNum1)
nultipleNum2 -= nultipleNum1;

如果,出现一个数是另外一个数的n倍,游戏结束。

当其中一个数是另外一个数的二倍以上(n倍)时,这时玩家可以根据逆推的方法选择减去n倍或者n-1倍,以达到让自己赢的情况。

五、代码

#include<iostream>

using namespace std;

int main()
{
int num1, num2;
cin >> num1 >> num2; while(num1 || num2)
{
int nultipleNum1, nultipleNum2;
nultipleNum1 = num1;
nultipleNum2 = num2;
int result = 0; //记录轮到谁,1:Singa,0:Suny while(nultipleNum1 && nultipleNum2) //如果两个数都大于0,游戏继续
{
result++;
result %= 2;
if(nultipleNum1 > nultipleNum2)
{
if(nultipleNum1 / nultipleNum2 >= 2) break; //当遇到一个数是另外一个数的两倍或两倍以上时,即能赢得游戏
else nultipleNum1 -= nultipleNum2;
}else
{
if(nultipleNum2 / nultipleNum1 >= 2) break;
else nultipleNum2 -= nultipleNum1;
}
} if(result == 1) cout << "Singa wins" << endl;
else cout << "Suny wins" << endl; cin >> num1 >> num2;
} return 0;
}

最新文章

  1. 转载文章——从HelloWorld学习操作系统
  2. tensorflow中的基本概念
  3. mybatis.xml文件中#与$符号的区别以及数学符号的处理
  4. Spark Idea Maven 开发环境搭建
  5. VS2010中项目配置引入GDAL
  6. python&amp;MongoDB爬取图书馆借阅记录(没有验证码)
  7. window窗口-button(按钮)-dialog(对话框,带按钮)
  8. job还是job
  9. IBM developerWorks 的Ajax系列教程
  10. 亲身体验:Vultr超高性价比VPS评测教程
  11. 个人作业3-(Alpha阶段)
  12. miniui几个常用知识点汇总
  13. 用pdf.js实现在移动端在线预览pdf文件
  14. CentOS 6.x 如何升级 glibc 2.17
  15. 大数据处理框架之Strom:redis storm 整合
  16. AIDL interface XXX should be declared in a file
  17. 实现Redis Cluster并实现Python链接集群
  18. 【Unity优化】怎样实现Unity编辑器中的协程
  19. 一:SpringIOC&amp;DI
  20. H5 获取地理位置

热门文章

  1. AJAX入门---点滴的积累
  2. bzoj1218: [HNOI2003]激光炸弹(DP二维前缀和)
  3. m_Orchestrate learning system---十五、如何快速查错
  4. CxImage 简单配置与使用
  5. Mysql实战45讲 05讲深入浅出索引(下)极客时间 读书笔记
  6. Opencv 编译
  7. JavaScript学习——使用JS完成注册页面表单校验
  8. STM8S103内存详析
  9. while循环,格式化输出%,运算符,数据类型的转换,编码的初识,
  10. 状压DP复习