坚持使用java分配,二进制搜索算法

时间:2013-09-10 05:08:42

标签: java binary-search

我在作业上达到了这一点,我希望得到一些指导。基本上程序应该让用户想到1-100之间的数字,然后询问它是否高于或低于50.然后程序输出中点直到范围,直到答案正确。例如,如果输入“h”,则会询问该数字是否为75,如果响应为“l”则会询问该数字是否为67,等等。

我认为我已经构建了框架,但我真的在努力寻找下一步找到中点。任何指导将不胜感激。

import java.util.Scanner;

public class numberguess 
{

    public static void main(String[] args) 
    {    
        String shouldPlayAgain = "y";
        String response = "h";

        Scanner keyboard = new Scanner(System.in);

        do
            {
                System.out.println("Guess a number between 1 and 100.");
                System.out.print("Is it 50? (h/l/c): ");
                response = keyboard.nextLine();

                    if (response.equals("h"))
                        {
                            System.out.println("Is it 75? (h/l/c): ");
                        }

                    if (response.equals("l"))
                        {
                            System.out.println("Is it 25? (h/l/c): ");
                        }

                System.out.print("Great! Do you want to play again? (y/n): ");
                shouldPlayAgain = keyboard.nextLine();
            }
        while (shouldPlayAgain.equals("y"));
        }
}       

3 个答案:

答案 0 :(得分:2)

我不会写出解决方案,但我会试着指出你正确的方向。希望这能让您在正确的轨道上自行实施解决方案。如果有任何不清楚的地方,请随时提出具体问题。

您需要创建两个变量来跟踪猜测的下限和上限。

int lowerBound = 0;
int upperBound = 100;

然后你迭代地猜测中间,即:

(upperBound + lowerBound) / 2;

假设你猜50。然后用户输入H。这意味着该数字大于50,但小于100.所以现在你知道你的新下界是50,设置:

lowerBound = 50;

重复这个过程,这次(upperBound + lowerBound) / 2;给你75,依此类推。你知道你猜测lowerBound是否等于upperBound。剩下的就是在循环中构建这个过程,你就完成了。

答案 1 :(得分:0)

基本上在您的问题中,您希望用户输入或考虑1-100之间的数字,并询问它是否高于或低于50.因此,您需要有两个变量,例如

int first = 0; and int last = 100;

获得中点直到答案正确

int middle = (first + last) / 2;

您可以在此处参考binary search algorithm了解详情。

希望它有所帮助!!

答案 2 :(得分:0)

包括

使用namespace std;

//函数原型

void playOneGame();

void getUserResponseToGuess(int guess,char& result);

int getMidPoint(int low,int high);

//主要方法

int main()

{

 //declare variables

 char response;

 //promt the users choice

 cout << "Ready to play (y/n)? ";

 cin >> response;

 //reapeat the loop until the user'c choice is no

 while (response == 'y')

 {

      //Call the function initially

      playOneGame();

      cout<<"Great! Do you want to play again (y/n)? ";

      cin >> response;

 }

 cout << "Thanks for playing!" << endl;

 system("pause");

 return 0;

}

//实现playOneGame函数

void playOneGame()

{

 //initialGuess

 int guess = 50;       // initial guess

 int low = 1;       // initial low point

 int high = 100;       // initial high point

 char result;

 cout<< "Think of a number between 1 to 100. " << endl;

//调用函数来猜测用户数//通过二进制搜索

 getUserResponseToGuess(guess, result);

 //Repeat the loop, until the answer is correct

 while (result != 'c')

 {

      //If the answer is high

      if (result == 'h')

      {

          low = guess;

          //compute the midpoint

          guess = getMidPoint(low, high);

          //call the function

          getUserResponseToGuess(guess, result);

      }

      else

      {

          high = guess;

          guess = getMidPoint(low, high);

          getUserResponseToGuess(guess, result);

      }

 }

}

//此功能输入计算机猜测并将其显示给用户。

void getUserResponseToGuess(int guess,char&amp; result)

{

 cout << "Is it " << guess << " (h/l/c)?"<< endl;

 cin >> result;

}

//此函数输入低和高,它返回//它们之间的中点。

int getMidPoint(int low,int high)

{

 return (low + high) / 2;

}