当前位置:网站首页>374. Guess the size of numbers (must be able to get started)
374. Guess the size of numbers (must be able to get started)
2022-07-19 03:29:00 【only-qi】
Problem description :
374. Guess the size of the numbers
The rules of the number guessing game are as follows :
Every round of the game , I'll start from 1 To n Randomly choose a number . Please guess which number is chosen .
If you guessed wrong , I'll tell you , Is your guess larger or smaller than the number I selected .
You can call a predefined interface int guess(int num) To get a guess , The total number of return values is 3 A possible situation (-1,1 or 0):
-1: The number I picked is smaller than your guess pick < num
1: The number I picked was bigger than you guessed pick > num
0: I picked the same number as you guessed . Congratulations ! You guessed it !pick == num
Back to the number I picked .
Example :
Example 1:
Input :n = 10, pick = 6
Output :6
Example 2:
Input :n = 1, pick = 1
Output :1
Example 3:
Input :n = 2, pick = 1
Output :1
Example 4:
Input :n = 2, pick = 2
Output :2
Tips :
1 <= n <= 231 - 1
1 <= pick <= n
Take the code and run it :
package com.onlyqi.test03.erfen;
import java.util.Scanner;
/**
* @author
* @date 2022/7/11 18:55
* @description
*/
public class Test02 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in); //scanner Instantiation
System.out.println(" Please enter what you want to enter n:");
int a=scan.nextInt();
System.out.println("====================="+guessNumber(a));
}
public static int guessNumber(int n) {
int low = 1;
int high = n;
while (low <= high) {
int mid = low + (high - low) / 2;
int res = guess(mid);
if (res == 0)
return mid;
else if (res < 0)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
public static int guess(int num){
int pick=66;
if(pick>num){
return 1;
}else if(pick<num){
return -1;
}else {
return 0;
}
}
}
Running results :

I want to brush it 300 Algorithm questions , The first 103 Avenue
边栏推荐
- Shell script receives and returns parameters
- 深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之ADASYN采样法]
- In depth understanding of machine learning - unbalanced learning: sample sampling technology - [smote sampling method and borderline smote sampling method of manual sampling technology]
- 374. 猜数字大小(入门 必会)
- Authentication code for wireless
- [MySQL] MHA high availability
- [NoSQL] redis high availability and persistence
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
- Dqn theoretical basis and code implementation [pytoch + cartpole-v0]
- Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
猜你喜欢

Shell script receives and returns parameters

Can't access this website can't find DNS address DNS_ PROBE_ What about started?

MySQL面试题(2022)

MYSQL主从搭建

数据源对象管理(第三方对象资源) & 加载properties文件

Code demonstration of fcos face detection model in openvino

laravel的问题

Flutter开发:运行flutter upgrade命令报错Exception:Flutter failed to create a directory at…解决方法

基于MFC如何实现单个文档的文件读写

神经网络学习笔记2.2 ——用Matlab写一个简单的卷积神将网络图像分类器
随机推荐
04_服务注册Eureka
Ncnn mat matrix class
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Yolov5 opencv DNN reasoning
数据源对象管理(第三方对象资源) & 加载properties文件
[MCU simulation] (XV) instruction system bit operation instructions - bit operation instructions, bit conditional transfer instructions
Wdog and power mode of fs32k148 commissioning
CorelDRAW 安装不了解决方法
oracle 查询 主机名和对应的IP地址
Win10 network connection shows no network but Internet access
ncnn paramdict&modelbin
Multi table query - case exercise
MySQL interview questions (2022)
We should increase revenue and reduce expenditure
通过OpenHarmony兼容性测评,大师兄开发板与丰富教培资源已ready
Use RZ, SZ commands to upload and download files through xshell7
Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]
XX市高中网络拓扑整体规划配置
代理模式——B站动力节点
05 central processing unit