1 ideas for solving problems
This is the most common number of guesses we have ever played.
It has a built-in method that can tell you the number is big or small.
In fact, is the second Division method. There's nothing to say. 2 Original Questions
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to N. You are have to guess which number I picked.
Every time for you guess wrong, I'll tell your whether the number is higher or lower.
You call a pre-defined API guess (int num) which returns 3 possible results ( -1, 1, or 0):
-1:my number is lower
1:my number is higher
0:congrats! You got it!
Example:
n = ten, I pick 6.
Return 6. 3 AC Solution
/* The Guess API is defined in the parent class Guessgame. @param num, your guess @return-1 If my number is lower, 1 if I am higher, otherwise return 0 int guess (i NT num);
* * public class Solution extends Guessgame {public int guessnumber (int n) {int start = 1;
int end = n;
int mid;
int tmp;
while (Start < end) {mid = Start/2 + END/2;
TMP = guess (mid);
if (TMP = = 0) return mid;
else if (tmp = = 1) {start = mid + 1;
} else{end = Mid-1;
} return start; }
}