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 95Sample Output 1:
Mary EE990830 Joe Math990112 6Sample Input 2:
1 Jean M AA980920 60Sample Output 2:
Absent Jean AA980920NA
这一题很简单,准备两个数组,一个放男学生,一个放女学生。然后排个序,按要求输出即可。
PS:从这一题可以明显看出姥姥的大女子注意!
代码如下:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define PRINT_I(x) printf("%s = %d\n",#x,x); #define PRINT_F(x) printf("%s = %f\n",#x,x); #define PRINT_S(x) printf("%s = %s\n",#x,x); typedef enum{ male, female }Gender; typedef struct { char name[12]; Gender gender; char id[12]; int grade; }Student; int cmp(const void *a,const void *b); int main(int argc,char* argv[]) { int i,j,n,grade,mc,fc,diff; char name[12],id[12]; char gnd; Student *mstu,*fstu; //freopen("input","r",stdin); scanf("%d",&n); mstu = (Student *)malloc(sizeof(Student)*n); fstu = (Student *)malloc(sizeof(Student)*n); mc = fc = 0; for(i=0;i<n;i++) { scanf("%s %c %s%d",name,&gnd,id,&grade); if(gnd == 'M') { strcpy(mstu[mc].name,name); mstu[mc].gender = male; strcpy(mstu[mc].id,id); mstu[mc].grade = grade; mc++; } else { strcpy(fstu[fc].name,name); fstu[fc].gender = female; strcpy(fstu[fc].id,id); fstu[fc].grade = grade; fc++; } } qsort(mstu,mc,sizeof(Student),cmp); qsort(fstu,fc,sizeof(Student),cmp); if(mc == 0) { printf("%s %s\n",fstu[fc-1].name,fstu[fc-1].id); printf("Absent\n"); printf("NA\n"); return 0; } if(fc == 0) { printf("Absent\n"); printf("%s %s\n",mstu[mc-1].name,mstu[mc-1].id); printf("NA\n"); return 0; } printf("%s %s\n",fstu[fc-1].name,fstu[fc-1].id); printf("%s %s\n",mstu[0].name,mstu[0].id); printf("%d\n",fstu[fc-1].grade-mstu[0].grade); free(fstu); free(mstu); return 0; } int cmp(const void *a,const void *b) { return ((Student *)a)->grade - ((Student *)b)->grade; }