Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
319692 | 王栎州 | 【C5-9】合影效果 | C++ | 解答错误 | 0 | 1 MS | 260 KB | 1497 | 2025-04-29 17:48:27 |
#include <iostream> #include <iomanip> struct Person { std::string gender; double height; }; // 交换两个 Person 对象 void swap(Person& a, Person& b) { Person temp = a; a = b; b = temp; } // 冒泡排序 void bubbleSort(Person arr[], int n) { for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - i - 1; ++j) { if ((arr[j].gender == "male" && arr[j + 1].gender == "female") || (arr[j].gender == "male" && arr[j + 1].gender == "male" && arr[j].height > arr[j + 1].height) || (arr[j].gender == "female" && arr[j + 1].gender == "female" && arr[j].height < arr[j + 1].height)) { swap(arr[j], arr[j + 1]); } } } } // 递归读取输入 void readInput(Person arr[], int n, int index = 0) { if (index < n) { std::cin >> arr[index].gender >> arr[index].height; readInput(arr, n, index + 1); } } // 递归输出结果 void printOutput(Person arr[], int n, int index = 0) { if (index < n) { std::cout << std::fixed << std::setprecision(2) << arr[index].height; if (index < n - 1) { std::cout << " "; } printOutput(arr, n, index + 1); } } int main() { int n; std::cin >> n; Person* people = new Person[n]; readInput(people, n); bubbleSort(people, n); printOutput(people, n); delete[] people; return 0; }