是
binary search的,这样做是最有效的方法。
Binary Search是你所描述的 对于1到N之间的数字
BinarySearch,
O(log(n))时间会运行。
所以这是找到1-N之间的数字的算法
int a = 1, b = n, guess = average of previous answers;while(guess is wrong) { if(guess lower than answer) {a = guess;} else if(guess higher than answer) {b = guess;} guess = (a+b)/2;} //Go back to while
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)