Different instructions take different numbers of cycles, so a processor's CPI depends on how often each kind of instruction occurs.
What to build
A program that computes the average CPI of an instruction mix and finds the class that contributes the most cycles.
Requirements
- Each class has a name, the percentage of instructions that belong to it, and the cycles per instruction.
- Print
CPI Xwith X to 2 decimal places. - Print
biggest NAMEfor the class with the largest contribution percent × cycles; on a tie the first one in the input wins.
Input
A line with n, then n lines, each name percent cycles. The percentages are whole numbers that add up to 100.
Output
Two lines.
Constraints
1 ≤ n ≤ 10; cycles are whole numbers from 1 to 100.
Example
Input:
4
ALU 50 1
load 20 5
store 10 3
branch 20 2
Output:
CPI 2.20
biggest load
Why it works
The weighted average multiplies each class's share of instructions by its cost and adds them. The class with the largest product is where most of the cycles go, which makes it the best target for improvement.
Examples
4 ALU 50 1 load 20 5 store 10 3 branch 20 2
CPI 2.20 biggest load
2 add 90 1 div 10 20
CPI 2.90 biggest div
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
CPI = the sum of (percent ÷ 100) × cycles over all instruction classes.
Hint 2
The cycles a class adds to the average is percent × cycles; the biggest of these is the class to improve first.
Hint 3
On a tie, keep the class that appears first in the input.
Solution
Show one correct solution (try it yourself first)
n = int(input())
total = 0
best_name, best = "", -1
for _ in range(n):
name, percent, cycles = input().split()
share = int(percent) * int(cycles)
total += share
if share > best:
best, best_name = share, name
print(f"CPI {total / 100:.2f}")
print("biggest", best_name)
▶ Open this solution in the compiler, then press Visualize to watch it run step by step