首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

有些想不通的一个解决方案。C++

2012-08-03 
有点想不通的一个解决方案。C++题目描述Two players, Singa and Suny, play, starting with two natural nu

有点想不通的一个解决方案。C++
题目描述
  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.


代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;

int cal(int a, int b, int step) {
  if (a < b) swap(a, b);
  if (a % b == 0 || a > b * 2) return step;
  else return cal(a - b, b, step + 1);
}

int main() {
  int a, b;
  while (scanf("%d%d", &a, &b) && a != 0) {
  int res = cal(a, b, 0);
  if (res % 2 == 0)
  printf("Singa wins\n");
  else
  printf("Suny wins\n");
  }
  return 0;
}  
我当时不是用这个方法做出来的,所以对里面的a > b * 2就可以直接判定答案,觉得很难想通。
我觉得
25 7
-> 11 7 
那如果是-> 18 7 
多了一步不就是另一个人赢了吗?
求指教。如果觉得很弱智也请见谅、

[解决办法]
it 's a kind of thought;

热点排行