一、题目链接
http://noi.openjudge.cn/ch0110/07/
二、解题思路
三、实施步骤
四、Java程序
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Student implements Comparable<Student> {
String sex;
double height;
public Student(String sex, double height) {
this.sex = sex;
this.height = height;
}
@Override
public int compareTo(Student o) {
return o.sex.compareTo(this.sex);
}
@Override
public String toString() {
return String.format("%.2f ", height);
}
}
public class Main {
public void sortASC(Student[] students, int from, int to) {
Arrays.sort(students, from, to + 1, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.height == o2.height) {
return 0;
}
return o1.height > o2.height ? 1 : -1;
}
});
}
public void sortDESC(Student[] students, int from, int to) {
Arrays.sort(students, from, to + 1, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.height == o2.height) {
return 0;
}
return o2.height > o1.height ? 1 : -1;
}
});
}
public void sort(Student[] students) {
Arrays.sort(students);
int from = 0;
int to = 0;
int n = students.length;
while (to < n && students[to].sex.equals("male")) {
to++;
}
to--;
sortASC(students, from, to);
sortDESC(students, to + 1, n - 1);
}
public static void main(String[] args) {
Main test = new Main();
Scanner input = new Scanner(System.in);
int n = input.nextInt();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
String sex = input.next();
double height = input.nextDouble();
students[i] = new Student(sex, height);
}
test.sort(students);
for (Student stu : students) {
System.out.print(stu.toString());
}
}
}