A multiplication game

Description
Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an integer 1 < n < 4294967295 and the winner is who first reaches p >= n.

Input
Each line of input contains one integer number n.

Output
For each line of input output one line either
Stan wins.
or
Ollie wins.
assuming that both of them play perfectly.

Sample Input

162
17
34012226

Sample Output

Stan wins.
Ollie wins.
Stan wins.

題意:兩個人輪流乘數字,從1開始,誰先乘到大於等於n爲勝。數字從2-9裏面選。
思路:這道題我是通過模擬找規律做的, 雖然說是2-9的選擇,但是可以直接用2和9來概括選擇,也就是說如果數比較小但在範圍之外,爲了不讓對方贏,一定會選擇2作乘數,而當數在範圍之內了,就會選擇9作乘數贏取遊戲。 那麼我們要做的就是找到這些範圍,也就是臨界點了。
列一段數據:
2-9 : Stan wins
10-18 : Ollie wins.
19-162 : Stan wins
163-324 : Ollie wins. ….
這樣就很明確了, 因爲乘數順序的原因,Stan 最大值乘以2就是下一個臨界點, 而Ollie的臨界點乘以9是下一個臨界點。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
int a[100];
int main()
{
  int n, j = 1;
  long long int i = 9;
  while(i < 4294967295)
  {
      a[j++] = i;
      if(j%2==0)
        i*=2;
      else
        i*=9;
  }
  while(cin >> n)
  {
      for(i = 0; i < j; i++)
      {
          if(n == a[i])
          {
              if(i%2 == 0)
                cout << "Ollie wins." << endl;
              else
                cout << "Stan wins." << endl;
              break;
          }
          if(n > a[i] && n <= a[i+1])
          {
              if(i%2 == 0)
                cout << "Stan wins." << endl;
              else
                cout << "Ollie wins." << endl;
              break;
          }
      }
  }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章