THINK FIRST·CODE LATER

← All labs

Leaderboard with a multi-key comparator

Problem

Read a class's exam results and print a leaderboard.

Input. An integer n (n ≥ 0), then n lines each with a name (one word, no spaces) and an integer score.

Output. If n is 0, print No students. and stop. Otherwise sort the students by score, highest first; students with equal scores are ordered by name in natural (alphabetical) order. Print one line per student: rank. name score. Tied students share a rank, and the next rank skips accordingly (1, 1, 3, …). Finally print Average: followed by the mean score rounded to one decimal place (use Math.round(avg * 10) / 10.0).

Build the ordering with Comparator.comparing, reversed and thenComparing, and compute the average with a stream.

Input:
4
Lina 92
Omar 85
Bea 92
Zed 70

Output:
1. Bea 92
1. Lina 92
3. Omar 85
4. Zed 70
Average: 84.8

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.