P8871 [ChuanZhi Cup #5 Preliminary] C - Lianzi’s Typesetting Design
Background
> You cannot rest now; deadlines are wandering around you.
Lianzi is rushing to finish her programming assignment. Besides writing the program code, it is also important to typeset the submitted assignment nicely to leave a good impression on the teaching assistant.
As everyone knows, code and some special kinds of text in an article need line numbers, but they are often very long, and adding them by hand is easy to make mistakes. So Lianzi decided to build her own tool and write a line number generator.
Description
Lianzi wants to implement the following function: given a text file as input, add line numbers to it.
Here are the definitions of three basic concepts in this problem:
- **Text characters** consist of all visible characters in $\textsf{ASCII}$, plus the space character ($\textsf{ASCII}=32$).
- **One line of characters** consists of several (possibly $0$) text characters, and **exactly one** newline character ($\textsf{ASCII}=10$) at the end.
- **A text file** consists of several (at least $1$) lines of characters. The number of lines in the text file is the number of lines that make it up.
Here is how to add line numbers in this problem:
- Suppose the text file has $m$ lines in total. Let the digit width of the positive integer $m$ be $s$. Then, at the beginning of each line, there will be a length of $s+1$ reserved for displaying the line number.
- For the $i$-th line, suppose the digit width of $i$ is $t$. Then the line number displayed for this line will be $\underbrace{\texttt{␣␣...␣}}_{s-t\text{ spaces}}\ i\ \texttt{␣}$, where $\texttt{␣}$ denotes a space.
Here is an example:
$$\boxed{\begin{aligned}
&\verb!#include!\\
&\verb!using namespace std;!\\
&\verb!int main(){!\\
&\verb! int a, b;!\\
&\verb! cin >> a >> b;!\\
&\verb! cout
Input Format
The input contains several lines, which form the original text file.
Output Format
The output contains several lines, which form the text file after adding line numbers.
Explanation/Hint
### Constraints and Notes
For all testdata, the total number of characters in the input text file (including newline characters) does not exceed $2\times 10^4$. Also, the input is guaranteed to be valid.
During judging, trailing spaces at the end of each line and the final newline at the end of the file will be ignored. Therefore, you may decide whether to output extra trailing spaces at the end of lines. For contestants using Java or Python, please pay attention to input/output efficiency.
Translated by ChatGPT 5