您的位置:首页 > 其它

PAT 1036. Boys vs Girls (25)

2014-02-27 18:37 483 查看


1036. Boys vs Girls (25)

时间限制

400 ms

内存限制

32000 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters
with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF-gradeM.
If one such kind of student is missing, output "Absent" in the corresponding line, and output "NA" in the third line instead.

Sample Input 1:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95

Sample Output 1:
Mary EE990830
Joe Math990112
6

Sample Input 2:
1
Jean M AA980920 60

Sample Output 2:
Absent
Jean AA980920
NA


提交代码

/*
满分 与 0分 的差别:scanf(" %c",&a); 输入字符前必须要加个空格!!!!!
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=100;
int cnt1,cnt2;
struct E{
char name[20];
char sex;
char no[20];
int grade;
};
bool cmp1(E a,E b)
{
return a.grade<b.grade;
}
bool cmp2(E c,E d)
{
return c.grade>d.grade;
}
int main()
{
int n,i;
//freopen("G:\\in.txt","r",stdin);
//freopen("G:\\our.txt","w",stdout);
while(scanf("%d",&n)!=EOF){
cnt1=0;cnt2=0;
E boy[MAX],girl[MAX];
char nameT[20],sexT,noT[20];
int gradeT;
for(i=0;i<n;i++){
scanf("%s %c%s%d",nameT,&sexT,noT,&gradeT);//输入字符前必须要加个空格!!!!!
if(sexT=='M'){
strcpy(boy[cnt1].name,nameT);
boy[cnt1].sex=sexT;
strcpy(boy[cnt1].no,noT);
boy[cnt1].grade=gradeT;
cnt1++;
}else{
strcpy(girl[cnt2].name,nameT);
girl[cnt2].sex=sexT;
strcpy(girl[cnt2].no,noT);
girl[cnt2].grade=gradeT;
cnt2++;
}
}
if(cnt2!=0){       //输出成绩最高的女生
sort(girl,girl+cnt2,cmp2);
printf("%s %s\n",girl[0].name,girl[0].no);
}else
printf("Absent\n");

if(cnt1!=0){       //输出成绩最低的男生
sort(boy,boy+cnt1,cmp1);
printf("%s %s\n",boy[0].name,boy[0].no);
}else{
printf("Absent\n");
}
if(cnt1!=0&&cnt2!=0)
printf("%d\n",girl[0].grade-boy[0].grade);
else
printf("NA\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: