用Map存储“题目-通过率”映射,用Collections.sort()排序键值,挨着打印即可:
import java.util.*; public class Main{ public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); Map<String, Double> m = new HashMap<>(); int n = Integer.parseInt(sc.nextLine()); for(int i = 0; i < n; ++i){ String[] v = sc.nextLine().split(" "); m.put(v[0], (double)Integer.parseInt(v[2])/(double)Integer.parseInt(v[1])); } List<String> r = new ArrayList<>(m.keySet()); Collections.sort(r); for(String s : r){ System.out.print(s); System.out.print(" "); System.out.println(f(m.get(s))); } } static int f(double x){ if(x <= 0.3) return 5; if(x > 0.6) return 3; return 4; } }