您的位置:首页 > 其它

PAT (Advanced) 1036. Boys vs Girls (25)

2018-03-03 14:59 330 查看
原题:1036. Boys vs Girls (25)

解题思路:
排两次序,按题意找出符合条件的人即可。

代码如下:#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000 + 5;
struct student
{
char name[15];
char course[15];
int grade;
int male;
} stu[maxn];

bool cmp1(student a, student b)
{
return a.male < b.male || (a.male == b.male && a.grade > b.grade);
}

bool cmp2(student a, student b)
{
return a.male > b.male || (a.male == b.male && a.grade < b.grade);
}

int main()
{
int n;
while(scanf("%d", &n) == 1)
{
if(n == 0) {printf("Absent\nAbsent\nNA\n"); continue;}
for(int i = 0; i < n; i++)
{
char m[2];
scanf("%s%s%s%d", stu[i].name, m, stu[i].course, &stu[i].grade);
if(m[0] == 'M') stu[i].male = 1;
else stu[i].male = 0;
}
sort(stu, stu+n, cmp1);
int score1 = -1, score2 = -1;
if(stu[0].male == 0) {printf("%s %s\n", stu[0].name, stu[0].course); score1 = stu[0].grade;}
else printf("Absent\n");
sort(stu, stu+n, cmp2);
if(stu[0].male == 1) {printf("%s %s\n", stu[0].name, stu[0].course); score2 = stu[0].grade;}
else printf("Absent\n");
if(score1 != -1 && score2 != -1) printf("%d\n", score1-score2);
else printf("NA\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: