解题分析:用node型vector向量储存输入数据,将Girls排在Boys前面,且Girls按grade降序排列,Boys按grade升序排列,具体输出照题目要求
注意:运用stl库自带的sort函数排序vector,应写成sort(num.begin(),num.end(),cmp)
#include"stdio.h"
#include<algorithm>
#include<vector>
using namespace std;
struct node {int grade;char c;char name[15], id[15];
};
bool cmp(node a, node b) {if (a.c == b.c&&a.c == 'M') return a.grade < b.grade;else if (a.c == b.c&&a.c == 'F') return a.grade > b.grade;else return a.c < b.c;
}
int main()
{int n, cnt1=0, cnt2=0;scanf("%d", &n);vector <node> num;node temp;for (int i = 0; i < n; i++) {scanf("%s %c %s %d", &temp.name, &temp.c, &temp.id, &temp.grade);if (temp.c == 'F') cnt1++;else cnt2++;num.push_back(temp);}sort(num.begin(), num.end(), cmp);bool flag = true;if (cnt1 == 0) printf("Absent\n");else printf("%s %s\n", num[0].name, num[0].id);if (cnt2 == 0) printf("Absent\n");else printf("%s %s\n", num[cnt1].name, num[cnt1].id);if (cnt1 == 0 || cnt2 == 0) printf("NA\n");else printf("%d\n", num[0].grade - num[cnt1].grade);return 0;
}