POJ2348:Euclid's Game

題意:有兩個人玩遊戲,遊戲內容如下:有兩個自然數,第一個人從較大的數中減去較小數的正整數倍,結果是非負數。兩人交替進行遊戲,誰先把較大的數變爲0,則勝利,遊戲結束。

解題思路:以m>= n爲例(m < n的情況與前面相同),當m=n時先手是必勝的;當m<n時,(m+n,n)肯定能到(m,n),並且這兩種狀態可肯定有一種是必敗,有着一種是必勝,那麼(m+kn,n)當k>=2時,肯定能夠到達上兩種情況的任意一種~~~,則爲必勝。

代碼如下~

#include <iostream>

using namespace std;
int check(int m,int n)
{
    if(n == 0)
    return 0 ;
    if(m / n >= 2)
    return 1 ;
    if(check(n , m % n))
    return 0 ;
    return 1 ;
}
int main()
{
    int a , b ;
    while(cin >> a >> b)
    {
        if(a == 0 && b == 0)
        break;
        if(a%b==0||b%a==0)
        cout << "Stan wins" << endl;
        else
        {
            if(a<b)
            {
                int t = a ;
                a = b ;
                b = t ;
            }
            if(check(a , b))
            cout << "Stan wins" << endl;
            else
            cout << "Ollie wins" << endl;
        }
    }
    return 0;
}


 

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