提交时间:2025-04-29 17:56:15
运行 ID: 319694
#include <iostream> #include <iomanip> using namespace std; struct Person { string gender; double height; }; void bubbleSortAsc(Person arr[], int size) { for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { if (arr[j].height > arr[j + 1].height) { Person temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // 冒泡排序降序 void bubbleSortDesc(Person arr[], int size) { for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { if (arr[j].height < arr[j + 1].height) { // 交换元素 Person temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int n; cin >> n; Person people[100]; Person males[100], females[100]; int maleIndex = 0, femaleIndex = 0; // 输入数据并分类 for (int i = 0; i < n; i++) { cin >> people[i].gender >> people[i].height; if (people[i].gender == "male") { males[maleIndex++] = people[i]; } else { females[femaleIndex++] = people[i]; } } bubbleSortAsc(males, maleIndex); bubbleSortDesc(females, femaleIndex); // 输出结果 cout << fixed << setprecision(2); for (int i = 0; i < maleIndex; i++) { cout << males[i].height << " "; } for (int i = 0; i < femaleIndex; i++) { cout << females[i].height; if (i != femaleIndex - 1) { cout << " "; } } cout << endl; return 0; }