You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.0 KiB
33 lines
1.0 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
int cmd(int a, int b) {
|
|
return a > b;
|
|
}
|
|
// https://blog.csdn.net/qq_40160605/article/details/80150252
|
|
/*
|
|
结果:
|
|
3 7
|
|
4 15
|
|
|
|
2 7
|
|
3 4
|
|
*/
|
|
int main() {
|
|
int a[6] = {1, 2, 4, 7, 15, 34};
|
|
sort(a, a + 6); //按从小到大排序
|
|
int p1 = lower_bound(a, a + 6, 7) - a; //返回数组中第一个大于或等于被查数的值
|
|
int p2 = upper_bound(a, a + 6, 7) - a; //返回数组中第一个大于被查数的值
|
|
cout << p1 << " " << a[p1] << endl;
|
|
cout << p2 << " " << a[p2] << endl;
|
|
|
|
puts("================================================");
|
|
|
|
sort(a, a + 6, cmd); //按从大到小排序
|
|
// int a[6] = {34,15,7,4,2,1};
|
|
int p3 = lower_bound(a, a + 6, 7, greater<int>()) - a; //返回数组中第一个小于或等于被查数的值
|
|
int p4 = upper_bound(a, a + 6, 7, greater<int>()) - a; //返回数组中第一个小于被查数的值
|
|
cout << p3 << " " << a[p3] << endl;
|
|
cout << p4 << " " << a[p4] << endl;
|
|
return 0;
|
|
} |