题解 | #输入n个整数,输出其中最小的k个#
输入n个整数,输出其中最小的k个
https://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int num,k; cin>>num; cin>>k; int tem; vector<int> store; while(cin>>tem){ store.push_back(tem); } sort(store.begin(),store.end()); for(int i =0;i<k;i++){ cout<<store[i]<<' '; } } // 应该用最小堆实现,有O(logn)时间复杂度