当前位置:网站首页>[C语言]限制查找次数,输出次数内查找到的最大值
[C语言]限制查找次数,输出次数内查找到的最大值
2022-06-11 17:59:00 【跳舞的皮埃尔】
1、题目
给你一个长度为 n 的数组 A。问你 m 次:在数组 A 中,小于等于给定的数xi的最大值是多大?
输入格式
- 第一行:两个整数 n 和 m,分别代表数组长度和查找次数。
- 第二行:n 个整数 ai,代表数组里每个数。
- 接下来 m 行,每行有 1 个整数 xi ,表示查找的整数。
输出格式
- 对于每次查询,如果可以找到,输出小于等于 xi 的最大值。
- 否则输出-1。
样例:
输入:
10 4
1 2 3 3 3 5 5 7 7 9
0
4
9
10
输出:
-1
3
9
9
2、完整代码
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
int a[100001];
int judge(int input,int a[]) {
int res;
// 查询找不到直接输出-1
if (input < a[0]) {
res = -1;
}
// 如果查找数比数组还长,直接输出排序后数组最后一个数
else if (input >= a[n - 1]) {
res = a[n - 1];
}
else if (input == a[0]) {
res = a[0];
}
// 二分查找
int ans = -1;
int left = 0, right = n - 1;
while (left <= right) {
// 右移一位
int mid = (left + right) >> 1;
if (a[mid] <= input) {
ans = a[mid];
left = mid + 1;
}
else {
right = mid - 1;
}
}
res = ans;
return res;
}
int main() {
// 数组长度和查找次数
cin >> n >> m;
// 输入完整数组
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// 数组排序,默认升序
sort(a, a + n);
for (int i = 0; i < m; i++) {
int x;
// 输入想查找的数
cin >> x;
cout << judge(x, a) << endl;
}
return 0;
}
3、截图

边栏推荐
- 【先收藏,早晚用得到】49个Flink高频面试题系列(二)
- Reading summary of nacos2.x source code
- jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
- sqli-labs通关嘿嘿~
- Upload labs failed to pass the customs halfway and the middle road collapsed
- Expérience initiale du moteur de segmentation de l'araignée mariadb
- How ZABBIX can customize MySQL monitoring items and trigger alarms
- LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
- zabbix怎样自定义mysql监控项并触发告警
- System learning typescript (V) - joint type
猜你喜欢
![[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)
MySQL/Redis 常见面试题汇总
![Winter vacation daily question 2022 [week1 not finished]](/img/56/a5ca98ddd128bcceb8e24f0edfeac6.jpg)
Winter vacation daily question 2022 [week1 not finished]

【先收藏,早晚用得到】100个Flink高频面试题系列(一)

SQL报错注入1

Getting started with Wireshark

SISO Decoder for Repetition(补充章节4)
![Spring 2021 daily question [week3 not finished]](/img/a4/72f2235d014613d26be0fc3524d236.jpg)
Spring 2021 daily question [week3 not finished]

SISO Decoder for min-sum(补充章节2)

神经网络与深度学习-2- 机器学习简单示例-PyTorch
随机推荐
SQL error injection 1
Rtsp/onvif protocol easynvr video platform arm version cross compilation process and common error handling
Spring 2021 daily question [end of week4]
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)
7-2 h0107. Pig-Latin
Feign 共享登录信息进行请求
New work of "the father of LSTM": a new method towards self correcting neural network
LDAP 目录服务器的现代化应用
vulhub
任意用户密码重置的10种方式
av_ read_ The return value of frame is -5 input/output error
ctf入门
Using packstack to quickly install openstack
Ffmpeg hardware codec NVIDIA GPU
SQL报错注入1
Sword finger offer (2nd Edition)
Tle6389-2g V50's unique pwm/pfm control scheme has a duty cycle of up to 100%, forming a very low differential pressure - keshijin mall
Spring 2021 daily question [week6 not finished]
Sqli labs customs clearance hey hey~
Can 400 fans earn 20W a month? How did you do it?