Guess

 

solution:

// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);

class Solution {
public:
    //注意的是guess函数到底是谁的数字。
    int guessNumber(int n) {
        //if(guess(n)==0) return n;//err.
        int left = 1, right = n, mid;
        while(left<=right)
        {
            mid = left + (right-left)*0.5;
            int t = guess(mid);
            if(t==1) left = mid+1;
            else if(t== -1) right = mid-1;
            else return mid;
        }
        return left;
    }
};

 

上一篇:TDD by example (5) -- 后招


下一篇:python入门第二讲--If-->else if