Real input often has several fields on each line.
What to build
A program that computes each student's average mark and finds the best student.
Requirements
- Read n, then n lines, each with a name and three whole-number marks.
- For each student print
NAME: AVGwith the average of the three marks shown with two decimals. - Finally print
Top: NAME (AVG). If several students share the top average, print the first one.
Input
Line 1: n. Then n lines name m1 m2 m3.
Output
n lines followed by one Top: line.
Constraints
1 ≤ n ≤ 30; marks are between 0 and 100; names are single words.
Example
Input:
3
Ada 90 80 70
Linus 100 95 90
Grace 60 70 65
Output:
Ada: 80.00
Linus: 95.00
Grace: 65.00
Top: Linus (95.00)
Why it works
The sum of the three marks is converted to float64 before dividing, so the average keeps its decimals. Comparing with > and not >= keeps the first student when averages are equal.
Examples
Example 1 · Input
3 Ada 90 80 70 Linus 100 95 90 Grace 60 70 65
Expected output
Ada: 80.00 Linus: 95.00 Grace: 65.00 Top: Linus (95.00)
Example 2 · Input
2 Tim 50 50 50 Ken 50 50 50
Expected output
Tim: 50.00 Ken: 50.00 Top: Tim (50.00)
Hints
Hint 1
Read each record with fmt.Scan(&name, &a, &b, &c).
Hint 2
Keep the best average and its name; update only when an average is strictly higher.
Solution
Show one correct solution (try it yourself first)
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
bestName := ""
best := -1.0
for i := 0; i < n; i++ {
var name string
var a, b, c int
fmt.Scan(&name, &a, &b, &c)
avg := float64(a+b+c) / 3
fmt.Printf("%s: %.2f\n", name, avg)
if avg > best {
best = avg
bestName = name
}
}
fmt.Printf("Top: %s (%.2f)\n", bestName, best)
}