Given the numbers 1 to 1000, what is the minimum number?

本文探讨了在给定1到1000的数字范围内,如何使用提示‘更高’或‘更低’来猜测特定数字时的最少和最多猜测次数。通过二分搜索方法,可以找到在1到1000之间的数字所需的最少猜测次数,并解释了如何计算最大猜测次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.programmerinterview.com/index.php/puzzles/minimum-guesses-1-100/

Given the numbers 1 to 1000, what is the minimum number of guesses needed to find a specific number if you are given the hint ‘higher’ or ‘lower’ for each guess you make?

This may be considered a trick question, since it’s deceptively easy. Read the question carefully and you’ll note that the question asks for the ‘minimum’ number of guesses. Think about it – someone can guess the right question on their first try right?

So, the answer here would be ‘1’, since it would take only one correct guess to find a specific number.

Finding the maximum number of guesses

But, what if we wanted to find the maximum number of guesses?

Well, think about this one. What if the number that you have to guess is ‘1’ and you start guessing from 1,000? Then, if the person who knows the number keeps saying lower, then you would guess 999,998,997…6,5,4,3,2, and finally until you get to 1.

The maximum number of guesses

This means that the maximum number of guesses is 999.

But, you must be thinking that’s a stupid answer – because no one would take that approach to guessing unless they were really foolish.

Using Binary Search to find a number from 1 to 1,000

The approach most programmers would take is by starting your guess in the middle of the set of numbers, and then continuing to divide the set of numbers in half with each guess. This approach to guessing (or “searching” for the number) is known as a binary search to most software engineers, and it is also known as a half-interval search. Let’s go through an example of how the binary search would work so that you can further understand the approach to solving this problem.

 

 

An example of using the binary search

So, let’s say the number you were trying to guess is a ‘1’. Then, you would start from the middle of 1,000 – which is 500. The person giving you hints would keep saying lower – and you would end up with something like this sequence of numbers to represent your guesses:

<pre="code">500, 250, 125, 63, 32, 16, 8, 4, 2, 1

Counting the number of guesses above would give you 10, which is our answer to the maximum number of guesses to find a number between 1 and 1000. In a binary search, if you take the log base 2 of the number of numbers (in this case, 1000), that would also give you the maximum number of guesses to find the correct number. So, if we take the log base 2 of 1,000 it would give us 9.965. Since you can’t possibly have a fraction of a guess, the result of log base 2 of 1000 should be rounded up to a whole number, which is 10, and the answer.

转载于:https://www.cnblogs.com/xue0/p/4479143.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值