编程语言
首页 > 编程语言> > PAT甲级 1036(C++)

PAT甲级 1036(C++)

作者:互联网

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct stu {
	string name, id;
	char s; int grade;
};
int N; vector<struct stu>info;
bool compare(struct stu s1, struct stu s2) {
	if (s1.s != s2.s) return s1.s<s2.s;
	else return s1.grade > s2.grade;
}
int main() {
	 cin >> N;
	 if (N == 0) {
		 cout << "Absent" << endl << "Absent" << endl << "NA";
		 return 0;
	 }
	 info.resize(N);
	 for (int i = 0; i < N; i++) {
		 cin >> info[i].name >> info[i].s >> info[i].id >> info[i].grade;
	 }
	 sort(info.begin(), info.end(), compare);
	 int flag[2] = { 0 }; 
	 if (info[0].s == 'F') flag[0] = 1;
	 if (info[N - 1].s == 'M') flag[1] = 1;
	 for (int i = 0; i < 2; i++) {
		 if (flag[i] == 0)
			 cout << "Absent" << endl;
		 else {
			 int index = (i == 0) ? 0 : N - 1;
			 cout << info[index].name << " " << info[index].id << endl;
		 }	 
	 }
	 if (flag[0] == 1 && flag[1] == 1) {
		 cout << info[0].grade - info[N - 1].grade;
	 }
	 else cout << "NA";
	 return 0;
}

标签:info,PAT,struct,int,s2,s1,C++,flag,1036
来源: https://blog.csdn.net/weixin_45681165/article/details/120769897