您的位置:首页 > 编程语言 > Java开发

【PAT甲级】1036. Boys vs Girls (25)——JAVA实现

2017-09-11 01:01 477 查看
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

题目大意:给定几个学生的姓名、性别、id和成绩,分别输出女生中成绩最好的学生的姓名和id以及男生中成绩最差的学生的姓名和id,并输出两者成绩之差(女减男),如果输入的数据缺少男或女同学的信息,则在缺的那行打印“Absent”来代替,最后行打印“NA”。

分析:该题较简单,方法很多,就提供一种使用Comparable接口的方法。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Student implements Comparable{
String name;
boolean man;
String id;
int grade;

public Student(){

}

public Student(String name, boolean man, String id, int grade) {
super();
this.name = name;
this.man = man;
this.id = id;
this.grade = grade;
}

@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Student stu = (Student)o;
return this.grade>stu.grade? 1:(this.grade==stu.grade?0:-1);
}
}

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

boolean flag = false;
int n,grade;
Student stu1=new Student(),stu2=new Student();
String name,id,gender;
ArrayList<Student> list1 = new ArrayList<Student>();//用来存放男生信息
ArrayList<Student> list2 = new ArrayList<Student>();//用来存放女生信息

n = input.nextInt();

for(int i=0;i<n;i++){
name = input.next();
gender = input.next();
id = input.next();
grade = input.nextInt();

if(gender.equals("M"))
list1.add(new Student(name,true,id,grade));
else
list2.add(new Student(name,false,id,grade));

}

Collections.sort(list1);
Collections.sort(list2);

if(list2.size()==0){
flag = true;
System.out.println("Absent");
}
else{
stu2 = list2.get(list2.size()-1);//女生找成绩最好的,即排序后最后一个元素
System.out.println(stu2.name + " " + stu2.id);
}

if(list1.size()==0){
flag = true;
System.out.println("Absent");
}else{
stu1 = list1.get(0);//男生找成绩最差的,即排序后第一个元素
System.out.println(stu1.name + " " + stu1.id);
}

if(flag){
System.out.println("NA");
}else{
System.out.println(stu2.grade-stu1.grade);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: