#include <iostream> #include <vector> #include <algorithm> #include <set> #include <sstream> using namespace std; int partition(vector<int> &arr, int i, int j) {     int I = i;     while (i < j)     {         while (i < j && arr[j] >= arr[I]) --j;         while (i < j && arr[i] <= arr[I]) ++i;         if (i < j) std::swap(arr[i], arr[j]);     }     std::swap(arr[i], arr[I]);     return i; } int get_least_k(vector<int> &vec_input, int k) {     int n = vec_input.size();     if (n == 0 || k <= 0 || k > n)         return 0;     int start_index = 0;     int end_index = n - 1;     int index = partition(vec_input, start_index, end_index);     while (index != k - 1) // 当分划元素的下标是k-1时,意味着前k-1个数比次数小(不大于),后边的数比次数大(不小于)     {         if (index > k - 1)             index = partition(vec_input, start_index, index - 1);         else if (index <= k - 1)             index = partition(vec_input, index + 1, end_index);     }     return vec_input[index]; } int main() {     vector<int> vec_num;     int val;     string line;     stringstream ss;     getline(cin, line);     ss.clear();     ss << line;     while (!ss.eof())     {         ss >> val;         vec_num.push_back(val);     }     int k;     cin >> k;     cout << get_least_k(vec_num,vec_num.size() + 1 - k) << endl;     return 0; }
点赞 3

相关推荐

01-03 12:06
复旦大学 Java
点赞 评论 收藏
分享
2025-12-04 15:23
三峡大学 FPGA工程师
不知道怎么取名字_:玩游戏都写到简历上了啊
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务