C++ in Xcode Given the numbers 1 to 1000, what is the minimum number of guesses
ID: 3797725 • Letter: C
Question
C++ in Xcode
Given the numbers 1 to 1000, what is the minimum number of guesses needed to find a specific number if you are given the hint ‘higher’ or ‘lower’ for each guess you make?
Read the question carefully and you’ll note that the question asks for the ‘minimum’ number of guesses. Think about it – someone can guess the right question on their first try right?
So, the answer here would be ‘1’, since it would take only one correct guess to find a specific number.
Finding the maximum number of guesses
But, what if we wanted to find the maximum number of guesses?
Explanation / Answer
Suppose we want the number 1000.
Initially we guessed is 1.
It shows higher then we guess 2.(suppose)
It again shows higher.
So again we guess 3.
and so on .....we get total of 1000 comparisions in maximum.
This is maximum case.
i.e.., for a given number n the maximum gueses would be n if n>=500.else 1000-n(coming from back).
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter the value to guess.[1,1000]"<<endl;
cin>>n;
cout<<"The maximum number of guesses would be:"<<endl;
if(n>=500)
cout<<n<<endl;
else
cout<<1000-n<<endl;
}
Output:Enter the value to guess.[1,1000]
687
The maximum number of guesses would be:
687
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.