#include <stdio.h> #include <string.h> typedef struct { int empId; char name[10]; int age; } Employee ; Employee employeeArray[30]; //j与j+1员工信息互换 void swap(int j) { int empId = 0, age = 0; char name[10] = {'\0'}; empId = employeeArray[j + 1].empId; age = employeeArray[j + 1].age; strcpy(name, employeeArray[j + 1].name); employeeArray[j + 1].empId = employeeArray[j].empId; employeeArray[j + 1].age = employeeArray[j].age; strcpy(employeeArray[j + 1].name, employeeArray[j].name); employeeArray[j].empId = empId; employeeArray[j].age = age; strcpy(employeeArray[j].name, name); } int main() { int n; scanf("%d ", &n); char data[n][100]; int i = 0, j = 0, cnt, idOrAge, nameSize; char name[10] = {'\0'}; while (gets(data[i])) { //printf("%s\n", data[i]); cnt = 1; idOrAge = 0; nameSize = 0; memset(name, 0, sizeof name); for (j = 0; j <= strlen(data[i]); j++) { if (data[i][j] == ' ' || j == strlen(data[i])) { if (cnt == 1) { employeeArray[i].empId = idOrAge; idOrAge = 0; cnt++; continue; } if (cnt == 2) { strcpy(employeeArray[i].name, name); cnt++; continue; } if (cnt == 3) { employeeArray[i].age = idOrAge; break; } } if (cnt == 1 || cnt == 3) { idOrAge = idOrAge * 10 + data[i][j] - '0'; } if (cnt == 2) { name[nameSize++] = data[i][j]; } } i++; } // //输出结构体数组 // for (i = 0; i < n; i++) { // printf("%d %s %d\n", employeeArray[i].empId, employeeArray[i].name, // employeeArray[i].age); // } //排序后输出最后三个职工信息 //先按姓名排序,再按工号,最后按年龄排序 for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (strcmp(employeeArray[j].name, employeeArray[j + 1].name) > 0) { //前者的名字更大,后移 swap(j); } } } //按工号排序 for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (employeeArray[j].empId > employeeArray[j + 1].empId) { //前者的工号更大,后移 swap(j); } } } //按年龄排序 for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (employeeArray[j].age > employeeArray[j + 1].age) { //前者的年龄更大,后移 swap(j); } } } //输出结构体数组 for (i = 0; i < 3; i++) { printf("%d %s %d\n", employeeArray[i].empId, employeeArray[i].name, employeeArray[i].age); } return 0; }