TreeMap Navigation
Completed
Store exam grade boundaries in a TreeMap<Integer, String>: 0→F, 50→D, 60→C, 70→B, 85→A. For each score, use floorEntry to print the grade, and print the next boundary above it with higherKey (or top).
Input
n, then n scores (0–100).
Output
n lines: grade next
Example
Input
4 42 60 84 99
Output
F 50 C 70 B 85 A top
Editor
Java
Hints
floorEntry(k) finds the greatest key <= k.
higherKey(k) finds the smallest key > k.
Solution explanation
Sorted maps answer range questions ("which band does this fall in?") without loops.
Show solution
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<Integer, String> grades = new TreeMap<Integer, String>();
grades.put(0, "F");
grades.put(50, "D");
grades.put(60, "C");
grades.put(70, "B");
grades.put(85, "A");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int score = sc.nextInt();
Integer next = grades.higherKey(score);
System.out.println(grades.floorEntry(score).getValue() + " " + (next == null ? "top" : next));
}
}
}