题解 | #输入n个整数,输出其中最小的k个#
输入n个整数,输出其中最小的k个
http://www.nowcoder.com/practice/69ef2267aafd4d52b250a272fd27052c
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int v,k;
while(cin>>v>>k)
{
priority_queue<int>big_head;
while(v--)
{
int data;
cin>>data;
if(big_head.size()<k)
{
big_head.push(data);
}
else
{
if(big_head.top()>data)
{
big_head.pop();
big_head.push(data);
}
}
}
stack<int>s;
while(!big_head.empty())
{
s.push(big_head.top());
big_head.pop();
}
while(!s.empty())
{
if(s.size()>1){
cout<<s.top()<<" ";
}
else{
cout<<s.top()<<endl;
}
s.pop();
}
}
return 0;
}