#include <bits/stdc++.h> // 引入所有标准库,不推荐使用 using namespace std; const int MAXN = 1e5+6; // 定义最大数组大小 int a[MAXN]; // 定义一个数组,大小为MAXN int main(){ int n,m; scanf("%d%d",&n,&m); // 输入n和m int i; for (i=0; i<n; i++) scanf("%d", &a[i]); // 输入n个数,存入数组a sort(a, a+n); // 对数组a进行排序 while(m--){ // 执行m次查询 int x; scanf("%d",&x); // 输入查询的数x if(x<a[0]) // 如果x小于a中最小的数 printf("%d\n", a[0]); // 输出a中最小的数 else if (x>a[n-1]) // 如果x大于a中最大的数 printf("-1\n"); // 输出-1 else { // 否则 int pos = lower_bound(a, a+n, x)-a; // 用lower_bound在a中查找第一个大于等于x的数,并返回其下标 printf("%d\n", a[pos]); // 输出该数 } } return 0;

#include bitsstdc++husing namespace std;const int MAXN = 1e5+6;int aMAXN;int main	int nm;	scanfdd&n&m;	int i;	for i=0; in; i++		scanfd &ai;	sorta a+n;	whilem--		int x;		scanfd&x;		ifxa0			printfdn a0;

原文地址: https://www.cveoy.top/t/topic/eZLb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录