A Boring Game

1539: A Boring Game

时间限制: 1 Sec  内存限制: 128 MB
提交: 3  解决: 3
[提交][状态][讨论版][命题人:外部导入]

题目描述

Alice has nothing to do,so she decided to paly a game. Firstly, she has three heaps of stone. The number of three heaps of stone are n1, n2, n3, respectively. At each round, she can choose two piles from the three heaps of stone, and then take a stone from each of the two piles to the heap not been choosed. For example, n1=1, n2=2, n3=3. After the first round, it can become n1=0, n2=1, n3=5 or n1=0, n2=4, n3=2 or n1=3, n2=1, n3=2. The game will end if and only if two heap of stones are empty. Now, Alice wants you to calculate the minimum rounds before the end of game.

输入

There are at most 100000 cases. For each line, there are three integer n1, n2, n3(1<=ni<=10000, i=1,2,3).

输出

For each case, print the minimum rounds. If the game will not end, print "Oh, my God!".

样例输入

1 2 3
1 2 2

样例输出

Oh, my God!
2


解题思路:找规律,列出公式,直接判断输出。如果用暴力搜索的话,需要开一个vis[10001][10001][10001]三维数组来标记每一堆石头,因为题目规定了1<=ni<=10000,而这样的三维数组太大,即使开在main函数外面,也会报错的,所以只能找规律了。
如果最小的那个数+3*j可以等于另外两个数中的一个的话,则有解,最小步数就是那个数的值。
#include <bits/stdc++.h>
using namespace std;

int Min=999999;

bool check(int x,int y,int z)
{
    if(x==y||x==z||y==z)
    {
        if(x==y||x==z)Min=x;
        else if(y==z)Min=y;
        return true;
    }
        int Maxn=(x>y?x:y)>z?(x>y?x:y):z;
        int Minm=(x<y?x:y)<z?(x<y?x:y):z;
        int Medi=x+y+z-Maxn-Minm;
        for(int j=0;j<=(Maxn-Minm)/3+0.5;++j)
        {
            if(Minm+3*j==Medi||Minm+3*j==Maxn)
            {
                if(Maxn<Min)Min=Maxn;
                return true;
            }
        }
        return false;
}

int main()
{
    int a,b,c;
    while(cin>>a>>b>>c)
    {
        Min=999999;
        if(check(a,b,c))cout<<Min<<endl;
        else cout<<"Oh, my God!"<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wjw2018/p/9320067.html