# C#LeetCode刷题之#374-猜数字大小（Guess Number Higher or Lower）

-1 : 我的数字比较小

1 : 我的数字比较大

0 : 恭喜！你猜对了！

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’ll tell you 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!

n = 10, I pick 6.

Return 6.

```public class Program {

public static void Main(string[] args) {
var n = 10;

var res = GuessNumber(n);
Console.WriteLine(res);

}

private static int Guess(int num) {
return 6.CompareTo(num);
}

private static int GuessNumber(int n) {
//二分法
var low = 1;
var high = n;
var mid = 0;
while(low <= high) {
mid = low + (high - low) / 2;
if(Guess(mid) == 1) {
low = mid + 1;
} else if(Guess(mid) == -1) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}

}```

`6`