Rounding each share separately often gives 99% or 101%. Use the largest remainder method so the labels on a chart add up to exactly 100.
What to build
A program that turns values into whole-number percentages that total 100.
Requirements
- Round every exact share down. Then give one extra point to each of the shares with the largest fractional parts until the total is 100; on equal fractional parts, earlier categories come first.
- Print
NAME P%for each category in input order.
Input
Line 1: n. Then n lines NAME VALUE.
Output
n lines.
Constraints
1 ≤ n ≤ 50; values are 0 or more and their total is over 0.
Example
Input:
3
a 1
b 1
c 1
Output:
a 34%
b 33%
c 33%
Why it works
Rounding down loses less than one point per category, so the missing points are fewer than n, and giving them to the largest remainders changes each share by the smallest possible amount.
Examples
3 a 1 b 1 c 1
a 34% b 33% c 33%
2 a 50 b 50
a 50% b 50%
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Compute each exact share, value ÷ total × 100, and round it down.
Hint 2
The number of missing points is 100 minus the sum of the rounded-down shares.
Hint 3
Sort positions by fractional part, largest first; Python's stable sort keeps input order on ties.
Solution
Show one correct solution (try it yourself first)
n = int(input())
rows = [input().split() for _ in range(n)]
total = sum(float(v) for _, v in rows)
exact = [float(v) / total * 100 for _, v in rows]
whole = [int(e) for e in exact]
order = sorted(range(n), key=lambda i: -(exact[i] - whole[i]))
for i in order[:100 - sum(whole)]:
whole[i] += 1
for (name, _), p in zip(rows, whole):
print(f"{name} {p}%")