本の目次を、番号と点線を入れて一行ずつ出力する。 入力の1行目に項目数 n と行の幅 W が空白1つ区切りで与えられる。 入力の続く n 行に、項目ごとに階層 d、ページ番号 p、題名がこの順に空白1つずつ区切りで与えられる。 題名は英字と数字からなる単語を空白1つずつ区切りでつないだ文字列で、題名の中にも空白があり得る。 最初の項目の階層は 1 で、各項目の階層は直前の項目の階層より大きくても 1 だけ大きい(小さくなるのはいくつでもよい)。 階層 1 から 6 まで階層ごとに数を1つずつ持ち、最初はすべて 0 である。 階層 d の項目が出たら、階層 d の数に 1 を足し、d より深い階層(d + 1 ~ 6)の数はすべて 0 に戻す。 その項目の番号は、そう変えた後の階層 1 から階層 d までの数を順に . でつないだ文字列である(例: 2.1.3)。 項目ごとに入力の順に1行を出力する。 左部分は、空白 2 × (d − 1) 個、番号、空白1つ、題名を順につないだ文字列である。 出力する行は、左部分、空白1つ、点(.)k 個、空白1つ、ページ番号 p を順につないだものである。 k は行全体の長さがちょうど W になる個数、つまり W − (左部分の長さ) − (p の桁数) − 2 である。 ただし、こうして求めた値が 3 より小さければ(0 や負のときも)k は 3 で、その行は W より長くなる。
入力
1 ≤ n ≤ 1,000、10 ≤ W ≤ 100、1 ≤ d ≤ 6、1 ≤ p ≤ 9,999(整数、先頭に 0 は付かない)。 ページ番号は増える順とは限らない。 題名は長さ 1 ~ 50 で、英字・数字・空白だけを使い、先頭と末尾は空白でなく、空白が2つ続くことはない
出力
n 行
例 1 · stdin
6 30
1 1 Intro
2 2 Why read
2 5 How to use
1 9 Basics
2 9 Values
3 12 Numbers and their shapes
stdout
1 Intro .................... 1
1.1 Why read ............. 2
1.2 How to use ........... 5
2 Basics ................... 9
2.1 Values ............... 9
2.1.1 Numbers and their shapes ... 12
例 2 · stdin
12 18
1 1 A
1 2 B
1 3 C
1 4 D
1 5 E
1 6 F
1 7 G
1 8 H
1 9 I
1 10 J
2 10 Ja
3 11 Jb
stdout
1 A ............ 1
2 B ............ 2
3 C ............ 3
4 D ............ 4
5 E ............ 5
6 F ............ 6
7 G ............ 7
8 H ............ 8
9 I ............ 9
10 J .......... 10
10.1 Ja ..... 10
10.1.1 Jb ... 11
import syslines = sys.stdin.read().split("\n")n, w = map(int, lines[0].split())count = [0] * 7 # count[d]: 단계 d에서 지금까지 센 번호out = []for i in range(1, n + 1): d, p, title = lines[i].rstrip("\r").split(" ", 2) d = int(d) count[d] += 1 for j in range(d + 1, 7): # 더 깊은 단계는 1부터 다시 센다 count[j] = 0 number = ".".join(str(count[j]) for j in range(1, d + 1)) left = " " * (d - 1) + number + " " + title k = w - len(left) - len(p) - 2 k = max(k, 3) # 모자라도 점은 3개 쓴다. 음수를 그냥 곱하면 빈 문자열이 된다 out.append(left + " " + "." * k + " " + p)print("\n".join(out))
JavaScriptmain.js · 20 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const [n, w] = lines[0].trim().split(" ").map(Number);const count = new Array(7).fill(0); // count[d]: 단계 d에서 지금까지 센 번호const out = [];for (let i = 1; i <= n; i++) { const line = lines[i].replace(/\r$/, ""); const a = line.indexOf(" "); const b = line.indexOf(" ", a + 1); const d = Number(line.slice(0, a)); const p = line.slice(a + 1, b); const title = line.slice(b + 1); count[d]++; for (let j = d + 1; j <= 6; j++) count[j] = 0; // 더 깊은 단계는 1부터 다시 센다 const number = count.slice(1, d + 1).join("."); const left = " ".repeat(d - 1) + number + " " + title; // 음수를 repeat에 넘기면 RangeError가 난다. 먼저 3과 견준다 const k = Math.max(w - left.length - p.length - 2, 3); out.push(left + " " + ".".repeat(k) + " " + p);}console.log(out.join("\n"));
TypeScriptmain.ts · 21 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const [n, w]: number[] = lines[0].trim().split(" ").map(Number);const count: number[] = new Array<number>(7).fill(0); // count[d]: 단계 d에서 지금까지 센 번호const out: string[] = [];for (let i = 1; i <= n; i++) { const line: string = lines[i].replace(/\r$/, ""); const a: number = line.indexOf(" "); const b: number = line.indexOf(" ", a + 1); const d: number = Number(line.slice(0, a)); const p: string = line.slice(a + 1, b); const title: string = line.slice(b + 1); count[d]++; for (let j = d + 1; j <= 6; j++) count[j] = 0; // 더 깊은 단계는 1부터 다시 센다 const num: string = count.slice(1, d + 1).join("."); const left: string = " ".repeat(d - 1) + num + " " + title; // 음수를 repeat에 넘기면 RangeError가 난다. 먼저 3과 견준다 const k: number = Math.max(w - left.length - p.length - 2, 3); out.push(left + " " + ".".repeat(k) + " " + p);}console.log(out.join("\n"));
JavaMain.java · 30 行
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)); String[] first = br.readLine().trim().split(" "); int n = Integer.parseInt(first[0]), w = Integer.parseInt(first[1]); int[] count = new int[7]; // count[d]: 단계 d에서 지금까지 센 번호 StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String[] part = br.readLine().split(" ", 3); // 제목 안의 빈칸은 그대로 둔다 int d = Integer.parseInt(part[0]); String p = part[1], title = part[2]; count[d]++; for (int j = d + 1; j <= 6; j++) count[j] = 0; // 더 깊은 단계는 1부터 다시 센다 StringBuilder number = new StringBuilder(); for (int j = 1; j <= d; j++) { if (j > 1) number.append('.'); number.append(count[j]); } String left = " ".repeat(d - 1) + number + " " + title; // 음수를 repeat에 넘기면 IllegalArgumentException이 난다. 먼저 3과 견준다 int k = Math.max(w - left.length() - p.length() - 2, 3); out.append(left).append(' ').append(".".repeat(k)).append(' ').append(p).append('\n'); } System.out.print(out); }}
C#Main.cs · 27 行
using System;using System.Text;class Program{ static void Main() { string[] first = Console.ReadLine().Trim().Split(' '); int n = int.Parse(first[0]), w = int.Parse(first[1]); var count = new int[7]; // count[d]: 단계 d에서 지금까지 센 번호 var sb = new StringBuilder(); for (int i = 0; i < n; i++) { string[] part = Console.ReadLine().TrimEnd('\r').Split(' ', 3); // 제목 안의 빈칸은 그대로 둔다 int d = int.Parse(part[0]); string p = part[1], title = part[2]; count[d]++; for (int j = d + 1; j <= 6; j++) count[j] = 0; // 더 깊은 단계는 1부터 다시 센다 string number = string.Join(".", count[1..(d + 1)]); // 단계 1 ~ d의 수 string left = new string(' ', 2 * (d - 1)) + number + " " + title; // 음수를 new string에 넘기면 ArgumentOutOfRangeException이 난다. 먼저 3과 견준다 int k = Math.Max(w - left.Length - p.Length - 2, 3); sb.Append(left).Append(' ').Append('.', k).Append(' ').Append(p).Append('\n'); } Console.Write(sb); }}
C++main.cpp · 29 行
#include <algorithm>#include <iostream>#include <string>int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, w; std::cin >> n >> w; int count[7] = {0}; // count[d]: 단계 d에서 지금까지 센 번호 std::string out; for (int i = 0; i < n; i++) { int d; std::string p, title; std::cin >> d >> p; std::cin.get(); // 쪽 번호 뒤의 빈칸 하나 std::getline(std::cin, title); // 제목 안의 빈칸은 그대로 둔다 if (!title.empty() && title.back() == '\r') title.pop_back(); count[d]++; for (int j = d + 1; j <= 6; j++) count[j] = 0; // 더 깊은 단계는 1부터 다시 센다 std::string number = std::to_string(count[1]); for (int j = 2; j <= d; j++) number += "." + std::to_string(count[j]); std::string left = std::string(2 * (d - 1), ' ') + number + " " + title; // 음수를 std::string(k, '.')에 넘기면 size_t로 바뀌어 length_error가 난다. 먼저 3과 견준다 int k = std::max(w - (int)left.size() - (int)p.size() - 2, 3); out += left + " " + std::string(k, '.') + " " + p + "\n"; } std::cout << out;}
Cmain.c · 34 行
#include <stdio.h>#include <string.h>int main(void) { int n, w; if (scanf("%d %d", &n, &w) != 2) return 0; int count[7] = {0}; /* count[d]: 단계 d에서 지금까지 센 번호 */ char title[64]; for (int i = 0; i < n; i++) { int d, p; scanf("%d %d ", &d, &p); fgets(title, sizeof title, stdin); /* 제목은 빈칸까지 줄 끝까지 읽는다 */ title[strcspn(title, "\r\n")] = '\0'; count[d]++; for (int j = d + 1; j <= 6; j++) count[j] = 0; /* 더 깊은 단계는 1부터 다시 센다 */ char left[128]; int len = 0; for (int j = 0; j < 2 * (d - 1); j++) left[len++] = ' '; for (int j = 1; j <= d; j++) { /* sprintf가 돌려주는 글자 수로 번호 길이를 센다(10 이상은 두 자리) */ len += sprintf(left + len, j > 1 ? ".%d" : "%d", count[j]); } len += sprintf(left + len, " %s", title); char page[8]; int plen = sprintf(page, "%d", p); int k = w - len - plen - 2; if (k < 3) k = 3; /* 모자라도 점은 3개 */ fputs(left, stdout); putchar(' '); for (int j = 0; j < k; j++) putchar('.'); printf(" %s\n", page); } return 0;}
Craykonmain.cray · 28 行
첫줄 = コンソール.行読み().空白除去().分ける(" ");n = 첫줄[0].数に();w = 첫줄[1].数に();번호 = 新リスト(7, 0); // 번호[d]: 단계 d에서 지금까지 센 번호繰り返す (i = 0; i < n; i += 1){ 조각 = コンソール.行読み().空白除去().分ける(" "); d = 조각[0].数に(); 쪽 = 조각[1]; 제목 = 조각.部分リスト(2, 조각.個数 - 2).つなぐ(" "); // 제목 안의 빈칸을 되살린다 번호[d] += 1; 繰り返す (j = d + 1; j <= 6; j += 1) { 번호[j] = 0; // 더 깊은 단계는 1부터 다시 센다 } 번호글 = "" + 번호[1]; 繰り返す (j = 2; j <= d; j += 1) { 번호글 = 번호글 + "." + 번호[j]; } 왼쪽 = " ".繰り返し(2 * (d - 1)) + 번호글 + " " + 제목; k = w - 왼쪽.長さ - 쪽.長さ - 2; もし (k < 3) { k = 3; // 음수를 되풀이에 넘기면 멈춘다. 모자라도 점은 3개 } コンソール.書く(왼쪽 + " " + ".".繰り返し(k) + " " + 쪽);}