首页 > 解决方案 > 猜谜游戏逻辑(二分查找)

问题描述

处理一个问题,我接受用户的输入 (1-10) 并猜测他们正在考虑使用二进制搜索的数字,并根据他们的答案更新范围(例如,如果它大于 5,我将 lowerLimit 更新为6)但我的逻辑有问题。

我使用中间单元格作为参考,当他们说中间单元格大于它时,将 1 添加到中间单元格,但我相信这是我感到困惑的地方。我不知道如何交织我的 if/else 语句以正确更新数字。

主要方法:

public class Main {
    public static void main(String[] args) {
        // test your program here
        GuessingGame game = new GuessingGame();
        game.play(1,10);
       
    }    
}

GuessingGame 方法(play 方法是我正在使用的方法):

import java.util.Scanner;


public class GuessingGame {
    
    private Scanner reader;
    

    public GuessingGame() {
        // use only this scanner, othervise the tests do not work
        this.reader = new Scanner(System.in);
       
             
    }

    public void play(int LL, int UL) {
       
       instructions(LL, UL);
       int limit = howManyTimesHalvable(UL - LL);
       int finalNumber = 0;
       int midPoint = average(LL, UL);
       
       int avgLL;
       int avgUL;
       
       
       
        
        
        
        for(int i = 0; i < limit; i++){
            
            
            if(isGreaterThan(midPoint)){
                midPoint++;
                LL = midPoint;
                
                midPoint = average(UL,LL);
                finalNumber = LL;
            }else{
                midPoint--;
                UL = midPoint;
                
                midPoint = average(UL,LL);
                finalNumber = LL;
               
             
            }
           
            
            if(UL == LL){
                break;
            }
            
        }
        System.out.println("Your number is : " + finalNumber);
     }
       
        
        
        
        

        

    

   public boolean isGreaterThan(int value){
       
       System.out.println("Is your number greater than " + value + "?");
       return reader.nextLine().equals("y");
   }
   
   public int average(int firstNumber, int secondNumber){
       int total =  firstNumber + secondNumber ; 
       return total / 2;
   }

    public void instructions(int lowerLimit, int upperLimit) {
        int maxQuestions = howManyTimesHalvable(upperLimit - lowerLimit);

        System.out.println("Think of a number between " + lowerLimit + "..." + upperLimit + ".");

        System.out.println("I promise you that I can guess the number you are thinking with " + maxQuestions + " questions.");
        System.out.println("");
        System.out.println("Next I'll present you a series of questions. Answer them honestly.");
        System.out.println("");
    }

    // a helper method:
    public static int howManyTimesHalvable(int number) {
        // we create a base two logarithm  of the given value

        // Below we swap the base number to base two logarithms!
        return (int) (Math.log(number) / Math.log(2)) + 1;
    }
}

当用户说他们猜测的数字高于或低于显示给他们的数字时,我想知道如何相应地更新范围。

编辑,示例条目:

寻找9号,

LL: 1 
UL: 10
limit:4 
finalNumber:0 
midPoint:5 
i: 0

Is your number greater than 5?
y

LL: 6
UL: 10
limit:4 
finalNumber:6
midPoint:8
i: 1

Is your number greater than 8?

LL: 9
UL: 10
limit:4 
finalNumber:9
midPoint:9
i: 2

Is your number greater than 9?
n


LL: 9
UL: 8
limit:4 
finalNumber:9
midPoint:8
i: 3

Is your number greater than 8?
y

Your number is : 9

标签: javabinary-search-tree

解决方案


你不应该有 midPoint--; 既然您问的问题是“大于”?如果答案是否定的,您的新上限应该是中点。


推荐阅读