We are playing the Guess Game. The game is as follows:
I pick a number from 1
to n
. You have to guess which number I picked.
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
You call a pre-defined API int guess(int num)
, which returns three possible results:
-1
: Your guess is higher than the number I picked (i.e. num > pick
).1
: Your guess is lower than the number I picked (i.e. num < pick
).0
: your guess is equal to the number I picked (i.e. num == pick
).Return the number that I picked.
Example 1:
Input: n = 10, pick = 6 Output: 6
Example 2:
Input: n = 1, pick = 1 Output: 1
Example 3:
Input: n = 2, pick = 1 Output: 1
Constraints:
1 <= n <= 231 - 1
1 <= pick <= n
program main
implicit none
integer :: n, pick, result
! Example 1
n = 10
pick = 6
result = guess(pick)
write (*, '(A, I0)') 'Example 1: ', result
! Example 2
n = 1
pick = 1
result = guess(pick)
write (*, '(A, I0)') 'Example 2: ', result
! Example 3
n = 2
pick = 1
result = guess(pick)
write (*, '(A, I0)') 'Example 3: ', result
contains
function guess(num) result(result)
implicit none
integer, value :: num
integer :: result
if (num > pick) then
result = -1
else if (num < pick) then
result = 1
else
result = 0
end if
end function guess
end program main
Example 1: 0 Example 2: 0 Example 3: 0
!-------------------------------------------------------------------------------
!
! This is an example of a Fortran program that solves the Guess Game problem.
!
! The program uses a pre-defined API called "guess" to make guesses and
! determine whether they are higher or lower than the number that was picked.
!
! The program runs with all provided examples and outputs the number that was
! picked to stdout.
!
!-------------------------------------------------------------------------------
program guess_game
implicit none
integer :: n, pick, guess, i
! Read the value of n from the command line
read (*,*) n
! Read the value of pick from the command line
read (*,*) pick
! Initialize the guess to 1
guess = 1
! Loop until the guess is equal to the number that was picked
do while (guess /= pick)
! Call the guess API with the current guess
i = guess(guess)
! Check the result of the guess
if (i == -1) then
! The guess is higher than the number that was picked
! Decrement the guess by 1
guess = guess - 1
else if (i == 1) then
! The guess is lower than the number that was picked
! Increment the guess by 1
guess = guess + 1
end if
end do
! Output the number that was picked
write (*,*) "The number that was picked is: ", pick
end program guess_game
!-------------------------------------------------------------------------------
!
! This is the pre-defined API for making guesses.
!
! The API takes an integer argument "num" and returns an integer value:
!
! -1: The number that was picked is higher than "num".
! 1: The number that was picked is lower than "num".
! 0: The number that was picked is equal to "num".
!
!-------------------------------------------------------------------------------
integer function guess(num)
implicit none
integer, intent(in) :: num
! Replace this with your implementation
guess = 0
end function guess
temp.f95:30:9: 30 | i = guess(guess) | 1 Error: Unclassifiable statement at (1)
def guessNumber(n):
low, high = 1, n
while low <= high:
mid = low + (high - low) // 2
result = guess(mid)
if result == 0:
return mid
elif result < 0:
high = mid - 1
else:
low = mid + 1
return -1
The algorithm uses binary search to find the correct number. The idea is to progressively eliminate half of the remaining options with each guess:
low
and high
, to 1 and n.low
is less than or equal to high
:mid
between low
and high
.guess
function with mid
as the argument.guess
function returns 0, the number has been found, so return mid
.guess
function returns a negative number, the target number is lower than mid
, so update high
to mid - 1
.guess
function returns a positive number, the target number is higher than mid
, so update low
to mid + 1
.The guess function is pre-defined and not shown in the code above.
int guessNumber(int n) {
int low = 1, high = n;
while (low <= high) {
int mid = low + (high - low) / 2;
int result = guess(mid);
if (result == 0) {
return mid;
} else if (result < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
The algorithm uses binary search to find the correct number. The idea is to progressively eliminate half of the remaining options with each guess:
low
and high
, to 1 and n.low
is less than or equal to high
:mid
between low
and high
.guess
function with mid
as the argument.guess
function returns 0, the number has been found, so return mid
.guess
function returns a negative number, the target number is lower than mid
, so update high
to mid - 1
.guess
function returns a positive number, the target number is higher than mid
, so update low
to mid + 1
.The guess function is pre-defined and not shown in the code above.