Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-07-26

GuessTheNumber

| 09:09

問題文

数字を推測。問題文の疑似コードを実装するだけ。

class GuessTheNumber {
public:
    int noGuesses(int upper, int answer) {
        int no = 1;
        int lower = 1;
        while (true) {
            int guess = (lower+upper) / 2;
            if (guess == answer) return no;
            else if (guess < answer) lower = guess+1;
            else if (guess > answer) upper = guess-1; 
            no++;
        }
        return -1;
    }
};