我们正在玩猜测游戏.游戏如下:我从1到n选一个数字.你必须猜出我选的数字.每次你猜错时,我会告诉你我选的数字是否高或低于你的猜测.你调用一个预定义的APIint guess(int num),它返回三个可能的结果: -1:你的猜测高于我选的数字 (即num > pick). 1:你的猜测低于我选的数字 (即num < pick 0).你的猜测等于我选的数字 (即num ==).返回我选的数字.
You can use binary search to efficiently guess the number I picked. Start by guessing the middle number between 1 and n. Depending on the response, you can eliminate half of the remaining numbers and guess again in the middle of the remaining range. Repeat this process until you correctly guess the number or until the remaining range becomes empty. Here's an example implementation:

```
int guessNumber(int n) {
    int left = 1, right = n;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        int res = guess(mid);
        if (res == 0) {
            return mid;
        } else if (res == 1) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1; // Error: not found
}
```