You are given n lines of song lyrics.
A lyric line may be an empty line with no characters at all, and it may start with several spaces.
Number only the lines that are not empty, 1, 2, 3, … from the top.
For a line that is not empty, print the number, a colon (:), one space, and then the content of the line, all on one line.
Print the content exactly as given, including spaces at the front and in the middle.
Print an empty line as an empty line, with no number.
On the last line, print the number of numbered lines and the number of empty lines, separated by one space.
In the example the first lyric line starts with two spaces, and the third and fifth lyric lines are empty.
Input
The first line has n (1 ≤ n ≤ 1,000, plain digits without a comma), and the next n lines have the lyrics, one line each. A lyric line has 0 to 80 characters and consists only of English letters, digits, spaces and . , ! ? ' . No line ends with a space (so no line is made of spaces only). The last lyric line may be empty
Output
n lines, one per lyric line, then a last line with the number of numbered lines and the number of empty lines
Example 1 · stdin
5
la la la
Twinkle, twinkle
How I wonder
stdout
1: la la la
2: Twinkle, twinkle
3: How I wonder
3 2
main.py
Error List00F8 ↓ · ⇧F8 ↑
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
The function that reads a whole line, leading spaces included, and counts an empty line as a line differs by language. Python's input(), Java's BufferedReader.readLine(), C#'s Console.ReadLine() and C++'s getline(cin, s) give one line without the newline. C's fgets keeps the newline, so remove it with buf[strcspn(buf, "\n")] = '\0'. In JS and TS, read the whole input and cut it with split("\n"). Craykon's Console.ReadLine() gives "" for an empty line and null only when the input has ended. If you read n on the first line with the same function and convert it (int(input()), atoi(buf), stoi(line), ToNumber()), mixing number reading with line reading can never shift the lines. The traps here are habits that are usually harmless. Trap one: the habitual input().strip() in Python removes the spaces at the front, so the first line of the example becomes 1: la la la. Trap two: Python's sys.stdin.readline(), unlike input(), keeps the newline. An empty line is "\n", so empty lines get numbers too and the last line of the example becomes 5 0. Trap three: in C, a space added to eat the leftover newline, as in scanf("%d ", &n), eats not one newline but every space and newline that follows. The first line of the example becomes 1: la la la, and if the lyrics begin with empty lines, those disappear as well. Trap four: cin >> n >> ws in C++ skips every following space and newline in the same way. Trap five: in JS, calling .trim() on the input before splitting removes the empty lines at the end. The fifth lyric line of the example becomes undefined and 4: undefined is printed. Trap six: in C#, Split('\n', StringSplitOptions.RemoveEmptyEntries) throws away every empty line, so there are too few lines, and on the example the program stops with IndexOutOfRangeException.
Pythonmain.py · 12 lines
n = int(input())numbered = 0empty = 0for _ in range(n): line = input() # 한 줄을 통째로: 앞 빈칸은 그대로, 줄 바꿈만 뗀다 if line == "": empty += 1 print() else: numbered += 1 print(f"{numbered}: {line}")print(numbered, empty)
JavaScriptmain.js · 18 lines
// 입력을 통째로 읽어 줄 바꿈으로만 나눈다. trim()을 하면 빈 줄과 앞 빈칸이 사라진다const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);let numbered = 0;let empty = 0;const out = [];for (let i = 1; i <= n; i++) { const line = lines[i]; if (line === "") { empty++; out.push(""); } else { numbered++; out.push(`${numbered}: ${line}`); }}out.push(`${numbered} ${empty}`);console.log(out.join("\n"));
TypeScriptmain.ts · 20 lines
import { readFileSync } from "node:fs";// 입력을 통째로 읽어 줄 바꿈으로만 나눈다. trim()을 하면 빈 줄과 앞 빈칸이 사라진다const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);let numbered = 0;let empty = 0;const out: string[] = [];for (let i = 1; i <= n; i++) { const line: string = lines[i]; if (line === "") { empty++; out.push(""); } else { numbered++; out.push(`${numbered}: ${line}`); }}out.push(`${numbered} ${empty}`);console.log(out.join("\n"));
JavaMain.java · 24 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int numbered = 0, empty = 0; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { String line = br.readLine(); // 빈 줄은 ""로 온다(null은 입력이 끝났을 때만) if (line.isEmpty()) { empty++; } else { numbered++; sb.append(numbered).append(": ").append(line); } sb.append('\n'); } sb.append(numbered).append(' ').append(empty); System.out.println(sb); }}
C#Main.cs · 28 lines
using System;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()!); int numbered = 0, empty = 0; var sb = new StringBuilder(); for (int i = 0; i < n; i++) { string line = Console.ReadLine()!; // 빈 줄은 ""로 온다(null은 입력이 끝났을 때만) if (line == "") { empty++; } else { numbered++; sb.Append(numbered).Append(": ").Append(line); } sb.Append('\n'); } sb.Append(numbered).Append(' ').Append(empty); Console.WriteLine(sb); }}
C++main.cpp · 23 lines
#include <iostream>#include <string>using namespace std;int main() { string line; // 첫 줄의 수도 getline으로 읽는다: cin >> n과 섞지 않으면 줄이 밀리지 않는다 getline(cin, line); int n = stoi(line); int numbered = 0, empty = 0; for (int i = 0; i < n; i++) { getline(cin, line); // 앞 빈칸까지 한 줄을 통째로, 줄 바꿈은 빼고 if (line.empty()) { empty++; cout << '\n'; } else { numbered++; cout << numbered << ": " << line << '\n'; } } cout << numbered << ' ' << empty << '\n'; return 0;}
Cmain.c · 24 lines
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void) { char buf[128]; /* 첫 줄의 수도 fgets로 읽는다: scanf("%d")와 섞지 않으면 줄이 밀리지 않는다 */ if (!fgets(buf, sizeof buf, stdin)) return 0; int n = atoi(buf); int numbered = 0, empty = 0; for (int i = 0; i < n; i++) { if (!fgets(buf, sizeof buf, stdin)) return 0; buf[strcspn(buf, "\n")] = '\0'; /* fgets가 담아 준 줄 바꿈을 지운다 */ if (buf[0] == '\0') { empty++; printf("\n"); } else { numbered++; printf("%d: %s\n", numbered, buf); } } printf("%d %d\n", numbered, empty); return 0;}
Craykonmain.cray · 18 lines
n = Console.ReadLine().ToNumber();numbered = 0;empty = 0;for (i = 0; i < n; i++){ line = Console.ReadLine(); // 빈 줄은 ""로 온다(빈값은 입력이 끝났을 때만) if (line == "") { empty += 1; Console.WriteLine(""); } else { numbered += 1; Console.WriteLine(numbered + ": " + line); }}Console.WriteLine(numbered + " " + empty);