mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-09-04 01:01:43 +00:00
update
This commit is contained in:
parent
a8ea175f77
commit
797976b853
55
src/test.cpp
55
src/test.cpp
@ -1,22 +1,41 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
using namespace std;
|
||||||
#include <algorithm>
|
|
||||||
|
const int N = 1000;
|
||||||
|
int c[N];
|
||||||
|
|
||||||
|
int logic(int x, int y) {
|
||||||
|
return (x & y) ^ ((x ^ y) | (~x & y));
|
||||||
|
}
|
||||||
|
|
||||||
|
void generate(int a, int b, int *c) {
|
||||||
|
for (int i = 0; i < b; i++)
|
||||||
|
c[i] = logic(a, i) % (b + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void recursion(int depth, int *arr, int size) {
|
||||||
|
if (depth <= 0 || size <= 1) return;
|
||||||
|
int pivot = arr[0];
|
||||||
|
int i = 0, j = size - 1;
|
||||||
|
while (i <= j) {
|
||||||
|
while (arr[i] < pivot) i++;
|
||||||
|
while (arr[j] > pivot) j--;
|
||||||
|
if (i <= j) {
|
||||||
|
int temp = arr[i];
|
||||||
|
arr[i] = arr[j];
|
||||||
|
arr[j] = temp;
|
||||||
|
i++; j--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recursion(depth - 1, arr, j + 1);
|
||||||
|
recursion(depth - 1, arr + i, size - i);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// 降序序列
|
int a, b, d;
|
||||||
std::vector<int> nums = {9, 7, 5, 3, 1};
|
cin >> a >> b >> d;
|
||||||
int x = 4;
|
generate(a, b, c);
|
||||||
|
recursion(d, c, b);
|
||||||
// 使用upper_bound查找第一个比x大的元素
|
for (int i = 0; i < b; ++i) cout << c[i] << " ";
|
||||||
// 第三个参数是目标值x
|
cout << endl;
|
||||||
// 第四个参数是自定义比较函数,保持降序的比较逻辑
|
|
||||||
auto it = std::upper_bound(nums.begin(), nums.end(), x, std::greater<int>());
|
|
||||||
|
|
||||||
if (it != nums.end()) {
|
|
||||||
std::cout << "第一个比" << x << "大的数是: " << *it << std::endl;
|
|
||||||
} else {
|
|
||||||
std::cout << "没有比" << x << "大的数" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user