三元组排序
思路分析
代码实现
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
pair[] arr = new pair[n];
int idx = 0;
while (n-- > 0) {
pair p = new pair();
p.a = sc.nextInt();
p.b = sc.nextDouble();
p.s = sc.next();
arr[idx++] = p;
}
Arrays.sort(arr);
for (pair r : arr) {
System.out.printf("%d %.2f %s\n", r.a, r.b, r.s);
}
}
}
class pair implements Comparable<pair> {
int a;
double b;
String s;
public int compareTo(pair o) {
return this.a - o.a;
}
}
同类题型
视频讲解
项目分区导航: 类与接口 ⬅️ | 01-三元组排序 | ➡️ 从尾到头打印链表
💬 评论