OJ刷题----- 寻找第 k 个最大元素
```cpp
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
int findmaxk(vector<int>& nums,int n, int k)
{
for (int last_position= n-1; last_position>=k-1; last_position--) {
int flag = 0;
for (int i = 0; i < last_position; i++)
{
if (nums[i] < nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
flag = 1;
}
}
if (flag == 0) break;
}
return nums[k-1];
}
int main()
{
int n,k;
cin >> n;
cin >> k;
//声明变长数组;
vector<int> nums(n);
for (int i = 0; i < n; i++)
{
cin >> nums[i];
}
int result = findmaxk(nums,n,k);
cout << result;
return 0;
}
运行结果: