找出1到100之间的数字的程序永远不会猜到"100".由于四舍五入 [英] Program to figure out number between 1 and 100 won't ever guess "100" because of rounding

查看:117
本文介绍了找出1到100之间的数字的程序永远不会猜到"100".由于四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用C ++.我写了一个小程序,选择一个1-100之间的随机数,然后对其进行修改,以使程序可以计算出该数字(并计算所需的猜测次数).

程序中的所有内容都可以正常工作,只有一件事除外.我正在使用一个公式来猜测当前猜测值与先前的最高/最低值之间的差异,因此对于一个太低的猜测:

low = guess;
guess = (( guess + high ) / 2);

它适用于除100以外的所有数字.当达到99时,它将199/2舍入为99,所以我得到了"99"猜测的无穷循环.有没有办法防止这种情况或某些公式可以解决此问题?我知道我可以使int high = 101或编写一个特例,如果程序第二次要猜测99,但这似乎不像是干净"的答案.

谢谢!

程序的完整代码:

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int randResult ( int low, int high )
{
    return rand() % ( high - low + 1 ) + low;
}

int main ()
{
srand( time ( NULL ));

int guess = 50; //set the initial guess
int high = 100;
int low = 1;
//int number = randResult( 1, 100 );
int number = 100;  //using this to test limits of guessing
int numberOfGuesses = 0;
bool guessCorrectly;

while ( guessCorrectly == 0 )
{
    cout << "Computer guessing " << guess << endl;
    numberOfGuesses++;

    if ( guess == number )
    {
        cout << "Correct!  The number was " << number << endl;
        guessCorrectly = 1;
    }
    else if ( guess < number )
    {
        cout << "Too low!" << endl;
        low = guess;
        guess = (( guess + high ) / 2);
    }
    else
    {
        cout << "Too high!" << endl;
        high = guess;
        guess = (( guess + low ) / 2 );
    }
}

cout << "Total Number of Guesses: " << numberOfGuesses << endl;
cout << "The Number Was: " << number << endl;

}

解决方案

另一种替代方法是从

开始

    int high= 101 ;

您永远不会要求101,因为在最坏的情况下,您会得到

    low= 99 ;
    high= 101 ;

然后

    guess= ( low + high ) / 2 ;   // = 100

I'm just getting started with C++. I wrote a small program that chooses a random number between 1-100, and then modified it to make the program figure out the number (and count the number of guesses required).

Everything in the program works, except for one thing. I'm using a formula that guesses the difference between the current guess and the previous highest/lowest value, so for a guess that's too low:

low = guess;
guess = (( guess + high ) / 2);

It works great for all numbers except for 100. When it gets to 99, it rounds 199/2 to 99, so I get an endless loop of "99" guesses. Is there a way to prevent this or some formula that would work around this? I know I could make int high = 101 or write a special case if the program is about to guess 99 a second time, but that doesn't seem like the "clean" answer to this.

Thanks!

Complete code of program:

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

int randResult ( int low, int high )
{
    return rand() % ( high - low + 1 ) + low;
}

int main ()
{
srand( time ( NULL ));

int guess = 50; //set the initial guess
int high = 100;
int low = 1;
//int number = randResult( 1, 100 );
int number = 100;  //using this to test limits of guessing
int numberOfGuesses = 0;
bool guessCorrectly;

while ( guessCorrectly == 0 )
{
    cout << "Computer guessing " << guess << endl;
    numberOfGuesses++;

    if ( guess == number )
    {
        cout << "Correct!  The number was " << number << endl;
        guessCorrectly = 1;
    }
    else if ( guess < number )
    {
        cout << "Too low!" << endl;
        low = guess;
        guess = (( guess + high ) / 2);
    }
    else
    {
        cout << "Too high!" << endl;
        high = guess;
        guess = (( guess + low ) / 2 );
    }
}

cout << "Total Number of Guesses: " << numberOfGuesses << endl;
cout << "The Number Was: " << number << endl;

}

解决方案

Another alternative is that you start with

    int high= 101 ;

You are never going to ask for 101, because in the worst case you will have

    low= 99 ;
    high= 101 ;

And then

    guess= ( low + high ) / 2 ;   // = 100

这篇关于找出1到100之间的数字的程序永远不会猜到"100".由于四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆