二分法猜数——C/C++
/*两个人A和B猜数,先输入一个n,A从1到n之间选一个数,B来猜这个数,
A会告诉B该数是大了还是小了,B至少要多少次才能确定A选的那个数呢?
*/
#include<stdio.h>
int main()
{
int n,res,cot=0;
scanf("%d%d",&n,&res);
int low = 1,high = n;
while(low<high){
int t = (low+high)/2;
cot++;
if(t<res){
low = t+1;
}else if(t>res){
high = t-1;
}else{
break;
}
}
printf("%d",cot);
return 0;
}