一、题目链接
http://noi.openjudge.cn/ch0110/01/
二、解题思路(Java)
三、解题思路(C++)
四、Java程序
import java.util.Arrays;
import java.util.Scanner;
class Student implements Comparable<Student> {
int id;
double score;
public Student(int id, double score) {
this.id = id;
this.score = score;
}
@Override
public int compareTo(Student o) {
if (o.score == this.score) {
return 0;
}
return o.score > this.score ? 1 : -1;
}
@Override
public String toString() {
return id + String.format(" %.1f", score);
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
Student[] students = new Student[n];
int id;
double score;
for (int i = 0; i < n; i++) {
id = input.nextInt();
score = input.nextDouble();
students[i] = new Student(id, score);
}
Arrays.sort(students);
System.out.print(students[k - 1].toString());
}
}
五、C++程序
#include <iostream>
#include <algorithm>
using namespace std;
struct student
{
int id;
double score;
};
int compare(student x, student y)
{
return x.score > y.score;
}
int main()
{
int n;
int k;
cin >> n;
cin >> k;
student stu[n];
for (int i = 0; i < n; i++)
{
cin >> stu[i].id;
cin >> stu[i].score;
}
sort(stu, stu + n, compare);
printf("%d %.1f", stu[k - 1].id, stu[k - 1].score);
return 0;
}