{ }codelper
paddingformatted outputP0096

Score table

The first line gives the number of students n, and each of the next n lines gives "name korean english math".
Print a table with five columns NAME, KOR, ENG, MATH, AVG.
AVG is the average of the three scores rounded to one decimal place (275 / 3 → 91.7, 171 / 3 → 57.0). - Each column's width is the length of the longest of its header and all values in that column. - The first line is the header, the second line joins, for each column, as many "-" as its width with "-+-", and then one line per student (in input order). - In the header line and the student lines, put " | " (space, vertical bar, space) between columns.
The NAME column is left-aligned (spaces after), the other columns are right-aligned (spaces before).
The header follows the same rule.

Input
1 ≤ n ≤ 20, a name is 1–15 English letters, 0 ≤ each score ≤ 100
Output
a table of n + 2 lines
Example 1 · stdin
3
Kim 90 85 100
Alexander 7 100 64
Bo 100 100 100
stdout
NAME      | KOR | ENG | MATH |   AVG
----------+-----+-----+------+------
Kim       |  90 |  85 |  100 |  91.7
Alexander |   7 | 100 |   64 |  57.0
Bo        | 100 | 100 |  100 | 100.0
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

First measure each column's width (including the header), then print aligned.
Forget the header and NAME overflows its column when every name is short.
Setting the width at run time differs by language: Python f"{s:<{w}}"/f"{s:>{w}}", C printf("%-*s", w, s)/"%*s" (pass the width for *), C++ std::left/std::right with std::setw(w), Java String.format("%-" + w + "s", s), JS/TS padEnd/padStart, C# PadRight/PadLeft (C#'s {0,-5} needs a constant width).
The average must divide as a real number, like / 3.0.
In C, C++, Java and C#, (a + b + c) / 3 is integer division, so 91.7 becomes 91.
Craykon aligns with PadRight(width)/PadLeft(width), makes one decimal with Str.Fixed(sum / 3, 1), and builds the dash line with "-".Repeat(w) and Join("-+-").

Pythonmain.py · 22 lines
n = int(input())headers = ["NAME", "KOR", "ENG", "MATH", "AVG"]rows = []for _ in range(n):    name, *scores = input().split()    a, b, c = map(int, scores)    rows.append([name, str(a), str(b), str(c), f"{(a + b + c) / 3:.1f}"]) # 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것widths = [max(len(r[i]) for r in [headers] + rows) for i in range(5)]  def line(cells):    # 이름은 왼쪽 맞춤, 나머지는 오른쪽 맞춤    parts = [f"{cells[0]:<{widths[0]}}"] + [f"{cells[i]:>{widths[i]}}" for i in range(1, 5)]    return " | ".join(parts)  print(line(headers))print("-+-".join("-" * w for w in widths))for r in rows:    print(line(r))
JavaScriptmain.js · 15 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const headers = ["NAME", "KOR", "ENG", "MATH", "AVG"];const rows = [];for (let i = 1; i <= n; i++) {  const [name, ...rest] = lines[i].trim().split(/\s+/);  const [a, b, c] = rest.map(Number);  rows.push([name, String(a), String(b), String(c), ((a + b + c) / 3).toFixed(1)]);}// 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것const widths = headers.map((_, i) => Math.max(...[headers, ...rows].map((r) => r[i].length)));// 이름은 왼쪽 맞춤(padEnd), 나머지는 오른쪽 맞춤(padStart)const line = (cells) => cells.map((s, i) => (i === 0 ? s.padEnd(widths[i]) : s.padStart(widths[i]))).join(" | ");const out = [line(headers), widths.map((w) => "-".repeat(w)).join("-+-"), ...rows.map(line)];console.log(out.join("\n"));
TypeScriptmain.ts · 16 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const headers: string[] = ["NAME", "KOR", "ENG", "MATH", "AVG"];const rows: string[][] = [];for (let i = 1; i <= n; i++) {  const [name, ...rest] = lines[i].trim().split(/\s+/);  const [a, b, c] = rest.map(Number);  rows.push([name, String(a), String(b), String(c), ((a + b + c) / 3).toFixed(1)]);}// 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것const widths: number[] = headers.map((_, i) => Math.max(...[headers, ...rows].map((r) => r[i].length)));// 이름은 왼쪽 맞춤(padEnd), 나머지는 오른쪽 맞춤(padStart)const line = (cells: string[]): string => cells.map((s, i) => (i === 0 ? s.padEnd(widths[i]) : s.padStart(widths[i]))).join(" | ");const out: string[] = [line(headers), widths.map((w) => "-".repeat(w)).join("-+-"), ...rows.map(line)];console.log(out.join("\n"));
JavaMain.java · 47 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.Locale; public class Main {    static int[] widths = new int[5];     public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(in.readLine().trim());        String[] headers = {"NAME", "KOR", "ENG", "MATH", "AVG"};        List<String[]> rows = new ArrayList<>();        rows.add(headers);        for (int i = 0; i < n; i++) {            String[] t = in.readLine().trim().split("\\s+");            int a = Integer.parseInt(t[1]), b = Integer.parseInt(t[2]), c = Integer.parseInt(t[3]);            // Locale.ROOT: 소수점이 쉼표인 지역 설정에서도 91.7로 찍게            String avg = String.format(Locale.ROOT, "%.1f", (a + b + c) / 3.0);            rows.add(new String[] {t[0], t[1], t[2], t[3], avg});        }        // 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것        for (String[] r : rows)            for (int i = 0; i < 5; i++) widths[i] = Math.max(widths[i], r[i].length());        StringBuilder out = new StringBuilder();        for (int k = 0; k < rows.size(); k++) {            out.append(line(rows.get(k))).append('\n');            if (k == 0) {                for (int i = 0; i < 5; i++) {                    if (i > 0) out.append("-+-");                    out.append("-".repeat(widths[i]));                }                out.append('\n');            }        }        System.out.print(out);    }     // 이름은 왼쪽 맞춤(%-Ns), 나머지는 오른쪽 맞춤(%Ns)    static String line(String[] cells) {        StringBuilder sb = new StringBuilder(String.format("%-" + widths[0] + "s", cells[0]));        for (int i = 1; i < 5; i++) sb.append(" | ").append(String.format("%" + widths[i] + "s", cells[i]));        return sb.toString();    }}
C#Main.cs · 30 lines
using System;using System.Collections.Generic;using System.Globalization;using System.Linq; class Program{    static void Main()    {        int n = int.Parse(Console.ReadLine());        string[] headers = { "NAME", "KOR", "ENG", "MATH", "AVG" };        var rows = new List<string[]> { headers };        for (int i = 0; i < n; i++)        {            string[] t = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);            int a = int.Parse(t[1]), b = int.Parse(t[2]), c = int.Parse(t[3]);            // InvariantCulture: 소수점이 쉼표인 문화권 설정에서도 91.7로 찍게            string avg = ((a + b + c) / 3.0).ToString("F1", CultureInfo.InvariantCulture);            rows.Add(new[] { t[0], t[1], t[2], t[3], avg });        }        // 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것        int[] widths = Enumerable.Range(0, 5).Select(i => rows.Max(r => r[i].Length)).ToArray();        // 이름은 왼쪽 맞춤(PadRight), 나머지는 오른쪽 맞춤(PadLeft)        string Line(string[] cells) =>            string.Join(" | ", cells.Select((s, i) => i == 0 ? s.PadRight(widths[i]) : s.PadLeft(widths[i])));        Console.WriteLine(Line(headers));        Console.WriteLine(string.Join("-+-", widths.Select(w => new string('-', w))));        foreach (var r in rows.Skip(1)) Console.WriteLine(Line(r));    }}
C++main.cpp · 34 lines
#include <algorithm>#include <iomanip>#include <iostream>#include <sstream>#include <string>#include <vector> int main() {    int n;    std::cin >> n;    std::vector<std::vector<std::string>> rows = {{"NAME", "KOR", "ENG", "MATH", "AVG"}};    for (int i = 0; i < n; i++) {        std::string name;        int a, b, c;        std::cin >> name >> a >> b >> c;        std::ostringstream avg;        avg << std::fixed << std::setprecision(1) << (a + b + c) / 3.0;        rows.push_back({name, std::to_string(a), std::to_string(b), std::to_string(c), avg.str()});    }    // 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것    std::vector<std::size_t> widths(5, 0);    for (const auto& r : rows)        for (int i = 0; i < 5; i++) widths[i] = std::max(widths[i], r[i].size());    auto print_row = [&](const std::vector<std::string>& r) {        // 이름은 왼쪽 맞춤(std::left), 나머지는 오른쪽 맞춤(std::right)        std::cout << std::left << std::setw(widths[0]) << r[0];        for (int i = 1; i < 5; i++) std::cout << " | " << std::right << std::setw(widths[i]) << r[i];        std::cout << '\n';    };    print_row(rows[0]);    for (int i = 0; i < 5; i++) std::cout << (i > 0 ? "-+-" : "") << std::string(widths[i], '-');    std::cout << '\n';    for (std::size_t k = 1; k < rows.size(); k++) print_row(rows[k]);}
Cmain.c · 40 lines
#include <stdio.h>#include <string.h> static char cells[21][5][16];static int widths[5]; // 이름은 왼쪽 맞춤(%-*s), 나머지는 오른쪽 맞춤(%*s) — *에 너비를 넘긴다static void print_row(int r) {    printf("%-*s", widths[0], cells[r][0]);    for (int i = 1; i < 5; i++) printf(" | %*s", widths[i], cells[r][i]);    printf("\n");} int main(void) {    int n, a, b, c;    const char *headers[5] = {"NAME", "KOR", "ENG", "MATH", "AVG"};    for (int i = 0; i < 5; i++) strcpy(cells[0][i], headers[i]);    scanf("%d", &n);    for (int r = 1; r <= n; r++) {        scanf("%15s %d %d %d", cells[r][0], &a, &b, &c);        sprintf(cells[r][1], "%d", a);        sprintf(cells[r][2], "%d", b);        sprintf(cells[r][3], "%d", c);        sprintf(cells[r][4], "%.1f", (a + b + c) / 3.0);    }    // 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것    for (int r = 0; r <= n; r++)        for (int i = 0; i < 5; i++) {            int len = strlen(cells[r][i]);            if (len > widths[i]) widths[i] = len;        }    print_row(0);    for (int i = 0; i < 5; i++) {        if (i > 0) printf("-+-");        for (int k = 0; k < widths[i]; k++) putchar('-');    }    printf("\n");    for (int r = 1; r <= n; r++) print_row(r);    return 0;}
Craykonmain.cray · 39 lines
// 이름은 왼쪽 맞춤(뒤채우기), 나머지는 오른쪽 맞춤(앞채우기)줄만들기(cells){    line = cells[0].PadRight(widths[0]);    for (i = 1; i < 5; i++)    {        line += " | " + cells[i].PadLeft(widths[i]);    }    return line;} n = Console.ReadLine().ToNumber();rows = [["NAME", "KOR", "ENG", "MATH", "AVG"]];for (r = 0; r < n; r++){    t = Console.ReadLine().Split();    합 = t[1].ToNumber() + t[2].ToNumber() + t[3].ToNumber();    rows.Add([t[0], t[1], t[2], t[3], Str.Fixed(합 / 3, 1)]);}// 칸 너비 = 머리글과 그 칸 값들 가운데 가장 긴 것widths = NewList(5, 0);foreach (row in rows){    for (i = 0; i < 5; i++)    {        widths[i] = Mathf.Max(widths[i], row[i].Length);    }}dashes = [];foreach (w in widths){    dashes.Add("-".Repeat(w));}Console.WriteLine(줄만들기(rows[0]));Console.WriteLine(dashes.Join("-+-"));for (r = 1; r < rows.Count; r++){    Console.WriteLine(줄만들기(rows[r]));}