作业帮 > 综合 > 作业

【紧急】讲解下这题的思路 ... (HUD)

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/04/30 00:28:18
【紧急】讲解下这题的思路 ... (HUD)
Guess Game
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Bob plays the "guess the number right" with Alice recently,the game's rule is that Alice give Bob a upper limit number N ,then he write any of a number on paper which Bob can't know what it is and the number must be between 1 and N.Bob has many chances to guess the number and every time when Bob guesses Alice will tell him if his is bigger than the correct number or small than the correct number until he is right.
Now the Bob wanted to use binary search method to guess the right number, because he knows this method is quite fast to find the right number. Input
We test the problem in many cases.Each case begin with one integers N( 1<= N <= 100000 ). Output
Output the expected number of chances required to guess the number right, which accurate to 2 fractional digits. Sample Input
2
3Sample Output
1.50
1.67Author
erriyue
对于楼上的我表示无语......
现在讲讲我的思路,首先这个题就是要用二分法找到这个数
先找1-N中间的数(1+N)/2
根据输入判断大小
然后就递归查找
看我的c语言查找
#include
#include
#define _N 100000/*注意是c语言写的*/
void search(long left,long right,long n)
{
long num=(left+right)/2;
if(num==n)
{
printf("\n\nLook!The number is %ld",num);
getch();
return;
}
else if(numn)
{
printf("%ld is too big!\n",num);
search(left,(left+right)/2,n);
}
}
int main()
{
long n;
printf("Alice,please input a number:");
scanf("%ld",&n);
search(1,_N,n);
return 0;
}