解题思路:
注意事项:
参考代码:
#include<stdio.h>
struct xs {
char sex[20];
float h;
};
struct xs x[100];
struct xs male[100];
struct xs female[100];
struct xs t;
int main() {
int n;
scanf("%d", &n) ;
int i, j = 0, k = 0, m;
for (i = 0; i < n; i++) {
scanf("%s %f", x[i].sex, &x[i].h);
}
//分类
for (i = 0; i < n ; i++) {
if (strcmp(x[i].sex, "male") == 0) {
male[j] = x[i];
j++;
} else {
female[k] = x[i];
k++;
}
}
// printf("\n");
// for (i = 0; i < j; i++) {
// printf("%.2f ", male[i].h);
// }
// for (i = 0; i < k; i++) {
// printf("%.2f ", female[i].h);
// }
// printf("\n");
//排序 男生
for (i = 0; i < j - 1 ; i++) {
for (m = 0; m < j -1; m++) {
if (male[m].h > male[m + 1].h) {
t = male[m];
male[m] = male[m + 1];
male[m + 1] = t;
}
}
}
//排序 女生
for (i = 0; i < k -1 ; i++) {
for (m = 0; m < k - i; m++) {
if (female[m].h < female[m + 1].h) {
t = female[m];
female[m] = female[m + 1];
female[m + 1] = t;
}
}
}
for (i = 0; i < j; i++) {
printf("%.2f ", male[i].h);
}
for (i = 0; i < k; i++) {
printf("%.2f ", female[i].h);
}
}
0.0分
0 人评分