数字猜谜游戏

时间:2013-10-30 20:28:42

标签: c++ algorithm

我得到了一个'破解算法'来修复。这是游戏'猜一个1-100之间的数字',计算机在7个问题/迭代内回答。

我得到的简报表明,只需要对算法进行微小的更改,对不起,如果这很模糊,我也在想同样的事情。

无论如何,这个算法充满了我已经清理过的愚蠢的错误。对于33的测试用例,算法分配以下中位数

50,25,37,19<< 19显然是不正确的。

我知道last_median =当前中位数不在正确的位置。这是一个漫长的一天,如果有人能够阐明这一点,我将不胜感激。

const int MAX_VALUE = 100;
int current_median = MAX_VALUE /2;
int last_median = 0;

while (true)
{
    last_median = current_median;

    if(number >= current_median)
    {
        if(number == current_median)
        { 
            //Check for equality
            cout << endl << number << endl; 
            break;
        } 

        current_median += last_median  /2;
    }
    else if(number <= current_median)
    {
        if(number == current_median)
        { 
            // Check for equality
            cout<<endl<<number<<endl; 
            break;
        }

        current_median -= last_median /2;
    }
}

4 个答案:

答案 0 :(得分:1)

两个提示:

  • 您可以在两个if s。
  • 之外移动相等比较
  • 您应该跟踪您知道的包含该号码的范围。一旦你这样做,逻辑将非常简单。

答案 1 :(得分:1)

该算法基本上是进行二分查找。你遇到的一个问题是你的条件:

if(number >= current_median)
// ...
else if(number <= current_median)
// ...

如果number相等,您想退出循环,而不是继续循环:

if(number > current_median)
// ...
else if(number < current_median)
// ...
else // we are equal
{
    break;
}

您还应调整条件以检查(low, high)范围,因为当您的中值接近实际值时,它应该(快速)变小。

一个简单的例子:

#include <iostream>

int main()
{
    std::cout << "Pick a number between [0, 100] (Don't tell me what it is!)";

    unsigned int low = 0;
    unsigned int high = 100;
    do
    {
        unsigned int median = (low + high) / 2;
        std::cout << "Is your number > " << median << "?  ";
        char answer;
        std::cin >> answer;
        if (answer == 'Y' || answer == 'y')
        {
            low = median;
            continue;
        }
        else
        {
            std::cout << "Is your number < " << median << "?  ";
            std::cin >> answer;
            if (answer == 'Y' || answer == 'y')
            {
                high = median;
                continue;
            }
            else // we are equal
            {
               low = high = median;
            }
        }

    } while (low != high);

    std::cout << "Your number is " << low << std::endl;
    return 0;
}

答案 2 :(得分:1)

如果您正在寻找0到100之间的数字,那么您将在7个或更少的步骤中找到它。如果你想要更精确,那么如果你正在寻找一个奇数,那么它正好是7个步骤,如果你正在寻找一个偶数,那么在某些情况下它将会是6或更少。我将在下面添加一个简单的代码。我没有把所有的支票(输入数字而不是字母或其他东西),它只是写在几分钟内:

#include <iostream>

int main(){

    std::cout << "Think of a number between 1 and 100" << std::endl ;
    std::cout << "Press 0 if my number is correct. 1 if YOUR number is smaller. 2 if bigger" << std::endl;

    int response = 0;
    int middle = 64;
    int low = 0;
    int high = 128;
    int counter = 1;

    while(response!=0)
    {
        std::cout << counter << " Guess " << counter << ": " << middle << std::endl;
        std::cout <<"Press 0(bingo)   1(smaller)   2(greater):   " ;
        std::cin >> response ;
        std::cout << std::endl ;
        counter++;
        switch (response){
        case 0:
            std::cout << "Your number is: " << middle << std::endl;
            break;

        case 1:
            high = middle;
            middle = (low+high)/2;
            break;

        case 2:
            low = middle;
            middle = (low+high)/2;
            break;

        } //end swhitch

    }; //end while


    return 0;
}

答案 3 :(得分:0)

int main()
{
char uput = '?';
int maxx = 100;
int minn = 0;

while (minn != maxx)
{
    cout << (minn+maxx)/2 << ": [h]igher, [l]ower or [e]qual?";
    cin >> uput;
    if (uput == 'l')
    {
        maxx = (minn+maxx)/2;
    }
    else if (uput == 'h')
    {
        minn = (minn+maxx)/2;
    }
    else if (uput == 'e')
    {
        cout << "Your number is " << (minn+maxx)/2 << ". Yay.";
        return 0;
    }
}
return 0;
}

如果数字低于中位数,则中位数是新的最大值。

如果数字高于中位数,则中位数是新的最小值。

创建一个循环。对于每次循环,找到minn和maxx之间的中位数。由于您每次缩小最小值和最大值之间的差距,最终只剩下1种可能性,这就是您的号码。

上面的代码并不是一个很好的例子,但希望它有助于说明。