简体   繁体   中英

Binary search a guessing number

My lecturer asked us to write a loop to ask the user to guess a random number in 1-100. When I tested the program, I found the fastest way to get the correct number is always guessing the middle number. Then I was told that this way is called binary search algorithm. So I'm thinking of writing another program to let the computer to guess the random number using binary search. Well, my program get the correct number, but there is a bug really bothers me which is that the count of guessing is 1 bigger. Can anyone give me a hint?

class Program
{
    static void Main(string[] args)
    {
        int start = 0;
        int end = 100;
        Random myRandom = new Random(); 
        int computer = myRandom.Next(start, end);
        int count = 1;
        int guess;
        guess = (end - start) / 2 + start;
        Console.WriteLine("Computer number: {0}, your guess is: {1}", computer, guess);
        Console.WriteLine("Count {0}", count);
        while (guess != computer)
        {
            guess = (end - start) / 2 + start;
            if (guess > computer)
            {
                end = guess;
                Console.WriteLine("Your guess is too high, next guess: {0}", guess);
            }
            else
            {
                start = guess;
                Console.WriteLine("Your guess is too low, guess again: {0}", guess);
            }
            count = count + 1;
            Console.WriteLine("Count {0}", count);
        }
        Console.WriteLine("You got it. The number is {0}. It took you {1} guesses.", computer, count);
        Console.ReadKey();
    }
}

This is the result, count should be 7 not 8

Computer number: 77, your guess is: 50
Count 1
Your guess is too low, guess again: 50
Count 2
Your guess is too low, guess again: 75
Count 3
Your guess is too high, next guess: 87
Count 4
Your guess is too high, next guess: 81
Count 5
Your guess is too high, next guess: 78
Count 6
Your guess is too low, guess again: 76
Count 7
Your guess is too low, guess again: 77
Count 8
You got it. The number is 77. It took you 8 guesses.

declaring

int count = 0;

instead of

int count = 1;

should solve the problem.

EDİT:

yea, you're right. If you move some of the code and delete some it will become like this

static void Main(string[] args)
{
    int start = 0;
    int end = 100;
    Random myRandom = new Random();
    int computer = myRandom.Next(start, end);
    int count = 0;
    int guess;

    while (true)
    {
        guess = (end - start) / 2 + start;
        count = count + 1;
        Console.WriteLine("Count {0}", count);
        if (guess > computer)
        {
            end = guess;
            Console.WriteLine("Your guess is too high, next guess: {0}", guess);
        }
        else if (guess < computer)
        {
            start = guess;
            Console.WriteLine("Your guess is too low, guess again: {0}", guess);
        }
        else
        {
            break;
        }
    }
    Console.WriteLine("You got it. The number is {0}. It took you {1} guesses.", computer, count);
    Console.ReadKey();
}

you don't have to write the code twice this way(as you had before the while and in the while)

You are writing the following duplicate loc outside the while loop:

guess = (end - start) / 2 + start;
Console.WriteLine("Computer number: {0}, your guess is: {1}", computer, guess);
Console.WriteLine("Count {0}", count);

while(guess != computer)

Here's the solution:

  1. You should initialize your guess to a value that does not fall between 0 and 100 ie, guess = 999 so that it enters the loop.

  2. You should remove the duplicate lines of code above the while.

  3. Initialize your count to 0 .

Here's the code for your reference:

int start = 0;
int end = 100;
Random myRandom = new Random();
int computer = myRandom.Next(start, end);
int count = 0;
int guess = 999;

while (guess != computer)
{
    guess = (end - start) / 2 + start;
    if (guess > computer)
    {
        end = guess;
        Console.WriteLine("Your guess is too high, next guess: {0}", guess);
    }
    else
    {
        start = guess;
        Console.WriteLine("Your guess is too low, guess again: {0}", guess);
    }
    count = count + 1;
    Console.WriteLine("Count {0}", count);
}
Console.WriteLine("You got it. The number is {0}. It took you {1} guesses.", computer, count);
Console.ReadKey();

Plus duplicating code is not a good coding practice.

Hope this helps.

I think that do is more accurate here than while . My version of your program:

int start = 0;
int end = 100;
Random myRandom = new Random();
int computer = myRandom.Next(start, end);
int count = 0;
int guess;
Console.WriteLine("Computer number: {0}", computer);
do {
    count++;
    Console.WriteLine("Count {0}", count);
    guess = (end - start) / 2 + start;
    if (guess > computer) {
        end = guess;
        Console.WriteLine("Your guess is too high, your guess: {0}", guess);
    }
    else if (guess < computer) {
        start = guess;
        Console.WriteLine("Your guess is too low, your again: {0}", guess);
    }
}
while (guess != computer);

Console.WriteLine("You got it. The number is {0}. It took you {1} guesses.", computer, count);
Console.ReadKey();

And the output is something like this:

输出

问题在于,在您第一次猜测之后(循环之外),您并没有更新startend ,因此它再次在猜测同一件事。

Add in a check before output and increment:

while (guess != computer)
{
    guess = (end - start) / 2 + start;
    if (guess != computer)
    {
        if (guess > computer)
        {
            end = guess;
            Console.WriteLine("Your guess is too high, next guess: {0}", guess);
        }
        else
        {
            start = guess;
            Console.WriteLine("Your guess is too low, guess again: {0}", guess);
        }
        count = count + 1;
        Console.WriteLine("Count {0}", count);
    }
}

count应该从0开始而不是1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM