#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int partion(vector<int>&v,int low,int high){
	int key = v[low];
	while(low < high){

		while(low<high && v[high]>= key){
			high--;
		}
		if(low<high){
			v[low++] = v[high];
		}

		while(low<high && v[low]<= key){
			low++;
		}
		if(low<high){
			v[high--] = v[low];
		}
	}
	v[low] = key;
	return low;
}

void getKnum(vector<int>&v,int k){
	if (v.size() == 0 || k>v.size())
	{
		return ;
	}
	int start = 0;
	int end = v.size()-1;
	int index = partion(v,start,end);

	while (index != k-1)
	{
		if (index > k-1)
		{
			end = index - 1;
			index = partion(v,start,end);
		}
		else
		{
			start = index + 1;
			index = partion(v,start,end);
		}
	}

	sort(v.begin(),v.begin()+k);
	for (int i=0;i<k;i++)
	{
		cout<<v[i]<<" ";
	}
}

int main(){
	vector<int>v;
	int n,t;
	while(cin>>t){
		v.push_back(t);

		if (getchar() == '\n')
		{
			break;
		}
	}
	int k = v[v.size()-1];
	v.pop_back();


	getKnum(v, k);
	return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐