题解 | #找到数组里的第k大数(C++)#
找到数组里的第k大数(C++)
https://www.nowcoder.com/practice/2d5e11b766654104ac91a54fe3a9f5db
#include<bits/stdc++.h> using namespace std; int main(){ int n,k; vector<int>a; // write your code here...... cin >> n; cin >> k; for (int i = 0;i < n;i++) { int num; cin >> num; a.push_back(num); } for (int i = 0;i < n;i++) { for (int j = 0;j < n - 1 - i;j++) { if (a[j] > a[j+1]) { swap(a[j],a[j+1]); } } } int cnt = 1; for(auto it : a) { if (cnt == k) { cout << it ; break; } cnt++; } return 0; }