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 Nlines 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 grade?F???grade?M??. 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
大體思路:
定義結構體陣列student,包含變數學生的姓名、性別、ID以及分數,定義一個結構體變數tempstu,按要求輸入n個學生的資訊,添加到student型的變長陣列stu中,將n個學生排序(女生在前,男生在后,分數均由高到低),如果stu[0]的性別為男,則說明無女生,如果stu[n-1]的性別為女,則說明無男生,按輸出要求輸出即可。
代碼:
/* 1036 Boys and Girls */
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct student{
char name[11];
char gender;
char id[11];
int score;
}tempstu;
bool cmp(student a,student b){
return a.gender!=b.gender?a.gender<b.gender:a.score>b.score;
}int main(){
int n;
scanf("%d",&n);
vector<student> stu;
for(int i=0;i<n;i++){
scanf("%s %c %s %d",&tempstu.name,&tempstu.gender,&tempstu.id,&tempstu.score);
stu.push_back(tempstu);
}
sort(stu.begin(),stu.end(),cmp);
if(stu[0].gender=='M')
printf("Absent\n%s %s\nNA\n",stu[n-1].name,stu[n-1].id);
else if(stu[n-1].gender=='F')
printf("%s %s\nAbsent\nNA\n",stu[0].name,stu[0].id);
else
printf("%s %s\n%s %s\n%d",stu[0].name,stu[0].id,stu[n-1].name,stu[n-1].id,stu[0].score-stu[n-1].score);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/113355.html
標籤:新技術前沿
下一篇:微信小游戲開發
