当前位置:网站首页>374. 猜数字大小(入门 必会)
374. 猜数字大小(入门 必会)
2022-07-17 01:00:00 【only-qi】
问题描述:
猜数字游戏的规则如下:
每轮游戏,我都会从 1 到 n 随机选择一个数字。 请你猜选出的是哪个数字。
如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。
你可以通过调用一个预先定义好的接口 int guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0):
-1:我选出的数字比你猜的数字小 pick < num
1:我选出的数字比你猜的数字大 pick > num
0:我选出的数字和你猜的数字一样。恭喜!你猜对了!pick == num
返回我选出的数字。
示例 :
示例 1:
输入:n = 10, pick = 6
输出:6
示例 2:
输入:n = 1, pick = 1
输出:1
示例 3:
输入:n = 2, pick = 1
输出:1
示例 4:
输入:n = 2, pick = 2
输出:2
提示:
1 <= n <= 231 - 1
1 <= pick <= n
上代码拿去即可运行:
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的实例化
System.out.println("请输入您要输入的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;
}
}
}
运行结果:

我要刷300道算法题,第103道
边栏推荐
- 04_服务注册Eureka
- [MCU simulation] (IV) addressing mode register addressing and direct addressing
- Chengxin University envi_ IDL second week class content: open hdf4 file and read the file, as well as simple data processing and saving + detailed analysis
- ES6 learning notes - brother Ma at station B
- [regression prediction] lithium ion battery life prediction based on particle filter with matlab code
- It's good to take more exercise
- 重写equals为什么要重写hashcode
- Snapshot: data snapshot (data disclosure method)
- 深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]
- Wechat applet
猜你喜欢

Bisenetv1 face segmentation

Solve the error of 0x00000709 when win10 connects to the shared printer

05_服务调用Ribbon

Redis和其他数据库的比较

Wechat applet -- Summary of problems in the actual development of taro framework
![[NoSQL] redis high availability and persistence](/img/73/53580db08b33c06c595fb3dfd6abce.png)
[NoSQL] redis high availability and persistence

Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2

Using gatekeeper to restrict kubernetes to create specific types of resources

Visual analysis of ncnn param file and bin model

JDBC连接Mysql数据库
随机推荐
JDBC连接Mysql数据库
Gdb+vscode for debugging 8 - use core to analyze dead cycles, deadlocks, and segment errors
XX市高中网络拓扑整体规划配置
[MCU simulation] (XXI) dB (define byte) - define byte
Theoretical basis of double Q-learning and its code implementation [pendulum-v0]
ES6學習筆記——B站小馬哥
Net SNMP related commands
Polynomial interpolation fitting (II)
GraphQL初识
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之ADASYN采样法]
Rewrite equals why rewrite hashcode
Use RZ, SZ commands to upload and download files through xshell7
Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]
Ncnn allocator memory allocator
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
Shell script receives and returns parameters
Affine transformation implementation
Vscode+ros2 environment configuration
MySQL replication table
重写equals为什么要重写hashcode