zajcen @ 2024-01-27 15:48:40
而且我在自己电脑vs上跑这个程序会莫名退出,请问是数组开太大了吗?
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
void quicksortkth(int a[], int s, int e, int k)
{
int x = a[s];
int i = s;
int j = e;
while (i != j) {
while (j > i && a[j] >= x)j--;
swap(a[i], a[j]);
while (i < j && a[i] <= x)i++;
swap(a[i], a[j]);
}
if (i == k) {
cout << a[i];
exit(0);
}
else if (i > k)quicksortkth(a, s, i - 1, k);
else quicksortkth(a, i + 1, e, k);//i比k还小,往右边搜
}
int main()
{
int n = 0, k = 0;
cin >> n >> k;
int a[5000005] = { 0 };
/*int* p = NULL;
p = (int*)malloc(n * sizeof(int));*/
for (int i = 0; i < n; i++) {
cin >> a[i];
}
quicksortkth(a, 0, n - 1, k);
}
by zajcen @ 2024-01-27 15:52:04
ok才发现原来要把大数组开在main外面...