PAT:A1036 Boys vs Girls (25 分)
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
代码:
C/C++:
#include<stdio.h>
struct Stu{
char name[15];
char sex;
char ID[15];
int grade;
}temp, boy, girl;
void init() {
boy.grade = 101;
girl.grade = -1;
}
int main() {
init();
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%s %c %s %d", temp.name, &temp.sex, temp.ID, &temp.grade);
if(temp.sex == 'M' && boy.grade > temp.grade) boy = temp;
else if(temp.sex == 'F' && girl.grade < temp.grade) girl = temp;
}
if(girl.grade == -1) printf("Absent\n");
else printf("%s %s\n", girl.name, girl.ID);
if(boy.grade == 101) printf("Absent\n");
else printf("%s %s\n", boy.name, boy.ID);
if(boy.grade == 101 || girl.grade == -1) printf("NA");
else printf("%d", girl.grade-boy.grade);
return 0;
}
Java:
import java.util.Scanner;
/**
* @author 余修文
* @date 2018/9/25 19:33
*/
public class Main {
static class Stu{
String name, ID;
String sex;
int grade;
public Stu(String name, String ID, String sex, int grade) {
this.name = name;
this.ID = ID;
this.sex = sex;
this.grade = grade;
}
public Stu(String name, String ID, String sex, String grade) {
this.name = name;
this.ID = ID;
this.sex = sex;
this.grade = Integer.valueOf(grade) ;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N;
Stu boy, girl, temp = null;
boy = new Stu(null, null, null, 101);
girl = new Stu(null, null, null, -1);
N = in.nextInt(); in.nextLine();
for(int i = 0; i < N; i++) {
String[ ] str =in.nextLine().split(" ");
temp = new Stu(str[0], str[1], str[2], str[3]);
//in.next();
if("M".equals(temp.sex) && boy.grade > temp.grade) {
boy = temp;
}
if("F".equals(temp.sex) && girl.grade < temp.grade) {
girl = temp;
}
}
if(girl.grade == -1) {
System.out.print("Absent\n");
} else {
System.out.println(girl.name + " " + girl.grade);
}
if(boy.grade == 101) {
System.out.print("Absent\n");
} else {
System.out.println(boy.name + " " + boy.grade);
}
if (girl.grade == -1 || boy.grade == 101) {
System.out.print("NA\n");
} else {
System.out.println( (girl.grade - boy.grade) );
}
}
}
博客围绕PAT的A1036题目展开,要求计算所有男学生最低成绩与所有女学生最高成绩的差异。给出了输入输出规范及示例,还提供了C/C++和Java代码来解决该问题。
405

被折叠的 条评论
为什么被折叠?



