标签:
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
思路:正常设置指针即可
1 #include<cstdio> 2 #include<cstring> 3 struct Student{ 4 char name[15]; 5 char gender[3]; 6 char Id[15]; 7 int grade; 8 }Student[1000]; 9 10 void Print(int pt) 11 { 12 if(pt==-1) 13 { 14 printf("Absent\n"); 15 } 16 else 17 { 18 printf("%s ",Student[pt].name); 19 // printf("%s ",Student[pt].gender); 20 printf("%s\n",Student[pt].Id); 21 // printf("%d\n",Student[pt].grade); 22 } 23 } 24 25 26 int main(int argc, char *argv[]) 27 { 28 int lowmale=101,highfemale=-1; 29 int lowpt=-1,highpt=-1; 30 int n; 31 scanf("%d",&n); 32 for(int i=0;i<n;i++) 33 { 34 scanf("%s",Student[i].name); 35 scanf("%s",Student[i].gender); 36 scanf("%s",Student[i].Id); 37 scanf("%d",&Student[i].grade); 38 if(strcmp("M",Student[i].gender)==0&&Student[i].grade<lowmale) 39 { 40 lowpt=i; 41 lowmale=Student[i].grade; 42 } 43 if(strcmp("F",Student[i].gender)==0&&Student[i].grade>highfemale) 44 { 45 highpt=i; 46 highfemale=Student[i].grade; 47 } 48 } 49 bool flag=true; 50 if(lowpt==-1||highpt==-1) 51 flag=false; 52 Print(highpt); 53 Print(lowpt); 54 if(!flag) 55 printf("NA\n"); 56 else 57 { 58 printf("%d\n",highfemale-lowmale); 59 } 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/GoFly/p/4271531.html