MayDay1 level
获赞
104
粉丝
2
关注
6
看过 TA
3
西南交通大学
2018
C++
IP属地:四川
暂未填写个人简介
私信
关注
2017-08-26 17:12
已编辑
西南交通大学 C++
去重之前,40%的通过率,用set去重之后60%的通过率!!!郁闷啊!!!请问大佬们100%的通过率 是怎么做到的啊 int main() { vector<int> a; set<int,greater<int>> b; int x; int k; while(getchar()!='\n') { cin>>x; a.push_back(x); b.insert(x); } // cin>>k; scanf("%d"...
yudw1316:#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; }
投递滴滴等公司10个岗位 >
0 点赞 评论 收藏
分享
2017-08-15 13:47
已编辑
西南交通大学 C++
0 点赞 评论 收藏
分享

创作者周榜

更多
关注他的用户也关注了:
牛客网
牛客企业服务