考察自定义结构体的排序
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { // 自定义结构体排序 BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(bf.readLine()); // int[][] list = new int[N][2]; // String[] name = new String[N]; Node[] nlist = new Node[N]; Set<String> set = new HashSet<String>(); for (int i = 0; i < N; i++) { set.clear(); String[] s = bf.readLine().split(" "); // name[i] = s[0]; // 输入的名字 int times = Integer.parseInt(s[1]); // list[i][1] = times; // 总共输入的数字 for (int j = 2; j < s.length; j++) { set.add(s[j]); } // list[i][0] = set.size(); // 不重复的标签的数量 nlist[i] = new Node(s[0], set.size(), times); } // 完成输入的功能 // for(Node e: nlist) { // System.out.println(e.name + " " + e.setLen + " " + e.allLen); // } // System.out.println(); Arrays.sort(nlist, new cmp()); // for(Node e: nlist) { // System.out.println(e.name + " " + e.setLen + " " + e.allLen); // } if(nlist.length >= 3) { for(int i=nlist.length-1; i>=nlist.length-3; i--) { if(i == nlist.length-1) { System.out.print(nlist[i].name); } else { System.out.print(" "+nlist[i].name); } } } else { if(nlist.length == 2) { for(int i=nlist.length-1; i>=nlist.length-2; i--) { if(i == nlist.length-1) { System.out.print(nlist[i].name); } else { System.out.print(" "+nlist[i].name); } } System.out.print(" " + "-"); } else if(nlist.length == 1) { System.out.print(nlist[0].name + " - -"); } } } static class cmp implements Comparator<Node>{ // 降序排列 public int compare(Node A, Node B) { if (A.setLen == B.setLen) { if (A.allLen < B.allLen) return 1; else return -1; } if (A.setLen > B.setLen) return 1; else return -1; } } static class Node { String name; int setLen; int allLen; Node(String name, int setLen, int allLen) { this.name = name; this.setLen = setLen; this.allLen = allLen; } } }
最后两个点超时了.