提交时间:2025-04-29 17:48:27
运行 ID: 319692
#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; }