一、题目链接
http://noi.openjudge.cn/ch0110/04/
二、解题思路(Java)
三、解题思路(C++)
四、Java程序
import java.util.Arrays;
import java.util.Scanner;
class Student implements Comparable<Student> {
int id;
int chs;
int math;
int en;
int total;
public Student(int id, int chs, int math, int en) {
this.id = id;
this.chs = chs;
this.math = math;
this.en = en;
this.total = chs + math + en;
}
@Override
public int compareTo(Student o) {
if (o.total > this.total) {
return 1;
}
else if (o.total < this.total) {
return -1;
}
else {
if (o.chs > this.chs) {
return 1;
}
else if (o.chs < this.chs) {
return -1;
}
else {
if (this.id > o.id) {
return 1;
}
else if (this.id < o.id) {
return -1;
}
else {
return 0;
}
}
}
}
@Override
public String toString() {
return id + " " + total;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
Student[] students = new Student[n];
int id;
int chs;
int math;
int en;
for (int i = 0; i < n; i++) {
id = i + 1;
chs = input.nextInt();
math = input.nextInt();
en = input.nextInt();
students[i] = new Student(id, chs, math, en);
}
Arrays.sort(students);
for (int i = 0; i < 5; i++) {
System.out.println(students[i].toString());
}
}
}
五、C++程序
#include <iostream>
#include <algorithm>
using namespace std;
struct student
{
int id;
int chs;
int math;
int en;
int total;
};
int cmp(student x, student y)
{
if (x.total != y.total)
{
return x.total > y.total;
}
else
{
if (x.chs != y.chs)
{
return x.chs > y.chs;
}
else
{
return x.id < y.id;
}
}
}
int main()
{
int n;
cin >> n;
student stu[n];
int i;
for (i = 0; i < n; i++)
{
stu[i].id = i + 1;
cin >> stu[i].chs;
cin >> stu[i].math;
cin >> stu[i].en;
stu[i].total = stu[i].chs + stu[i].math + stu[i].en;
}
sort(stu, stu + n, cmp);
for (i = 0; i < 5; i++)
{
cout << stu[i].id << " " << stu[i].total << endl;
}
return 0;
}