TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%

普通的二分查找。


Problem Statement

 

A popular guessing game is "Guess the number", where one person selects a number in a known range, and another person tries to guess that number. After each guess, the first person reveals whether the guess was correct, too high or too low.

Pretty soon one learns the best strategy, which is to guess the middle number among those not yet ruled out. If there is no single middle number, then there are two numbers to choose from. In that case, we choose the smallest of those numbers.

The algorithm can be described like this:

Init lower and upper bound
Repeat
  x = (lower bound + upper bound)/2  (round down if necessary)
  Make guess x
  If x is too low, set lower bound to x+1
  If x is too high, set upper bound to x-1
Until x is correct

For instance, assume that the lower and upper bound initally are 1 and 9, respectively. The middle number in this range is 5. If this is "too low", the new bounds become 6 and 9. This range contains four numbers, and there is thus no single middle number. The two numbers in the middle are 7 and 8, and the smallest of these is 7, so our next guess then becomes 7.

Create a class GuessTheNumber which contains the method noGuesses which takes an intupper, the initial upper bound of the range (the inital lower bound is always 1), and an intanswer, the number selected by the first person. The method should return an int representing the total number of guesses required for the second person to guess the correct number using the method described above.

Definition

 
Class:GuessTheNumber
Method:noGuesses
Parameters:int, int
Returns:int
Method signature:int noGuesses(int upper, int answer)
(be sure your method is public)
 
 

Constraints

-upper will be between 1 and 1000, inclusive.
-answer will be between 1 and upper, inclusive.

Examples

0) 
 
9
6
Returns: 3

The first guess will be (1+9)/2=5, which is too low. The new lower bound then becomes 5+1=6.

The second guess then becomes (6+9)/2=7, which is too high. The new upper bound then becomes 7-1=6.

The third guess is then of course (6+6)/2)=6, which is correct. So, three guesses were required, and the method thus returns 3.

1) 
 
1000
750
Returns: 2
The first guess will be 500, the second guess 750.
2) 
 
643
327
Returns: 7
The guesses are 322, 483, 402, 362, 342, 332 and finally 327, so the method returns 7.
3) 
 
157
157
Returns: 8
Here the guesses are 79, 118, 138, 148, 153, 155, 156 and finally 157. The method thus returns 8.
4) 
 
128
64
Returns: 1
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

public class GuessTheNumber {


	public  int noGuesses(int upper, int answer) {
		int num = 1;
		int lower = 1;
		int mid = 0;

		while (lower <= upper) {
			mid = (upper + lower) / 2;
			if (answer == mid)
				return num;
			num++;
			if (mid < answer) {
				lower = mid + 1;
			} else
				upper = mid - 1;
		}
		return num;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值