-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimu.cpp
More file actions
73 lines (51 loc) · 1.47 KB
/
Copy pathtimu.cpp
File metadata and controls
73 lines (51 loc) · 1.47 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
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
// 分区函数,选择最后一个元素作为基准
int partition(vector<int>& arr, int low, int high) {
int pivot = arr[high]; // 选择最后一个元素作为基准
int i = low - 1; // 指向小于基准的区域的末尾
for (int j = low; j <= high - 1; j++) {
// 如果当前元素小于或等于基准
if (arr[j] <= pivot) {
i++; // 扩展小于基准的区域
swap(arr[i], arr[j]); // 将当前元素放到区域内
}
}
// 将基准放到正确位置
swap(arr[i + 1], arr[high]);
return i + 1; // 返回基准的索引
}
// 快速排序递归函数
void quickSort(vector<int>& arr, int low, int high) {
if (low < high) {
// 分区并获取基准位置
int pi = partition(arr, low, high);
// 递归排序基准左侧和右侧的子数组
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
vector<int> arr;
string input;
// 读取整行输入
getline(cin, input);
// 使用字符串流解析输入
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
// 检查是否输入了数字
if (arr.empty()) {
cout << "未输入任何数字!" << endl;
return 1;
}
int n = arr.size();
quickSort(arr, 0, n - 1);
cout<<arr[n];
return 0;
}