If we are told to guess a number between 5 and 5 inclusive, clearly we only need 1 guess since there is only one possibility - 5. If we have to guess a number between 5 and 7 inclusive, we might guess it in one guess it

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

A program in java 

If we are told to guess a number between 5 and 5 inclusive, clearly we only need 1 guess
since there is only one possibility - 5.
If we have to guess a number between 5 and 7 inclusive, we might guess it in one guess if
we are lucky, but in the worst case we can still guess it in 2 guesses. This is contingent
upon being told whether or not we guessed too high or too low.
Our first guess could be 6. If we guessed correctly that is only one guess. If we guess
either too low or too high, we only have one possible value to guess from remaining - 5 or
7. Our maximum number of guesses is therefore 2.
Given an inclusive range of values to guess from, calculate the maximum number of
guesses we will take to guarantee we guess any selected value providing we guess
efficiently.
Input Format
Input is the low and high values representing our range from which to guess.
Constraints
Both the low and high values will be valid java integers. High will be greater or equal to
low.
Output Format
Single integer value representing the maximum number of guesses we will need.
Sample Input 0
79
Sample Output 0
2
Explanation 0
first guess is 8, second will be either 7 or 9. Two guesses maximum for three possible
choices.
Sample Input 1
39
Sample Output 1
3
Explanation 1
first guess is 6, our remaining range is now either 3 - 5 or 7-9, and we saw that 3
possibilities requires 2 guesses. So, three guesses maximum.
1 import java.io.*;
2 import java.util.*;
3
import java.text.*;
4 import java.math. *;
5 import java.util.regex.*;
6
7
public class Solution {
8
9 ▾
10
11
12
13 }
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
// your code goes here
}
Java 8
Transcribed Image Text:If we are told to guess a number between 5 and 5 inclusive, clearly we only need 1 guess since there is only one possibility - 5. If we have to guess a number between 5 and 7 inclusive, we might guess it in one guess if we are lucky, but in the worst case we can still guess it in 2 guesses. This is contingent upon being told whether or not we guessed too high or too low. Our first guess could be 6. If we guessed correctly that is only one guess. If we guess either too low or too high, we only have one possible value to guess from remaining - 5 or 7. Our maximum number of guesses is therefore 2. Given an inclusive range of values to guess from, calculate the maximum number of guesses we will take to guarantee we guess any selected value providing we guess efficiently. Input Format Input is the low and high values representing our range from which to guess. Constraints Both the low and high values will be valid java integers. High will be greater or equal to low. Output Format Single integer value representing the maximum number of guesses we will need. Sample Input 0 79 Sample Output 0 2 Explanation 0 first guess is 8, second will be either 7 or 9. Two guesses maximum for three possible choices. Sample Input 1 39 Sample Output 1 3 Explanation 1 first guess is 6, our remaining range is now either 3 - 5 or 7-9, and we saw that 3 possibilities requires 2 guesses. So, three guesses maximum. 1 import java.io.*; 2 import java.util.*; 3 import java.text.*; 4 import java.math. *; 5 import java.util.regex.*; 6 7 public class Solution { 8 9 ▾ 10 11 12 13 } public static void main(String[] args) { Scanner in = new Scanner (System.in); // your code goes here } Java 8
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education