Dark_yet_bright @ 2024-03-26 22:47:30
相同的题在acwing测试点全过,洛谷上一个都运行不起来,谢谢大佬们
#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n, k, sj[5000010];
int res;
int findk(int* nums, int l, int r, int k)
{
if (l >= r)
return nums[l];
int partition = nums[l], i = l, j = r;
while (i < j) //等于时会停止进行交换
{
while (nums[j] >= partition&& i < j)
j--;
while (nums[i] <= partition && i < j)
i++;
if (i < j)
{
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
int tmp = nums[l];
nums[l] = nums[j];
nums[j] = tmp;
if (j == k)
{
return nums[j];
}
if(j>k)
findk(nums, l, j-1, k);
else
findk(nums, j+1, r, k);
}
int main()
{
cin >> n >> k;
for (int i = 0;i < n;i++)
{
scanf("%d",sj[i]);
}
res=findk(sj, 0, n - 1, k);
cout << res;
}
by TimSwn090306 @ 2024-03-26 23:06:20
@S296685913 scanf 要加取址符号 &。
by Dark_yet_bright @ 2024-03-26 23:20:41
@TimSwn090306 抱歉我伞兵了,忘记在讨论中加了,我原来有还是这样,还是报错
by TimSwn090306 @ 2024-03-27 11:54:43
@S296685913 你 findk 函数最后一个 ifelse 里面的 findk 前都应该加 return 。
不然函数没有返回值会报错。(UB
by Dark_yet_bright @ 2024-03-27 20:28:50
@TimSwn090306 哈哈,脑袋混了,谢谢指点,没注意,主要它居然能在acw上全部ac就放松警惕了,很离谱,不知道为什么acw上全过,谢谢了哈