-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminKNum.cpp
More file actions
65 lines (55 loc) · 1.22 KB
/
minKNum.cpp
File metadata and controls
65 lines (55 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/****
* 每个测试输入包含空格分割的n+1个整数,最后一个整数为k值,n
* 不超过100。
* ***/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int getPartition(vector<int>& vec, int low, int high)
{
int key = vec[low];
while (low < high)
{
while (low < high && vec[high] >= key)
high--;
vec[low] = vec[high];
while (low < high && vec[low] < key)
low++;
vec[high] = vec[low];
}
vec[low] = key;
return low;
}
int main()
{
vector<int> vec;
int tmp;
while (scanf("%d", &tmp))
{
if (tmp == 0)
break;
vec.push_back(tmp);
}
int k = vec.back();
vec.pop_back();
int low = 0;
int high = vec.size()-1;
int index = getPartition(vec, 0, vec.size());
while (index != k-1)
{
if (index < k-1)
{
low = index+1;
index = getPartition(vec, low, high);
}
else if (index > k-1)
{
high = index-1;
index = getPartition(vec, low, high);
}
}
sort(vec.begin(), vec.begin()+5);
for (int i = 0; i <= index; i++)
cout << vec[i] << " ";
}