import sysdef main(): out = [] physical = 0 start = 0 parts = [] open_line = False # 앞줄이 역슬래시로 끝나 아직 이어지는 중인가 for line in sys.stdin: # 입력이 끝나면 반복도 끝난다 line = line.rstrip("\n") # 줄바꿈만 뗀다. strip()은 앞뒤 빈칸까지 지운다 physical += 1 if not open_line: start = physical parts = [] if line.endswith("\\"): parts.append(line[:-1]) # 마지막 역슬래시 하나만 뗀다 open_line = True else: parts.append(line) out.append(f"{start} [{''.join(parts)}]") open_line = False if open_line: # 마지막 줄이 역슬래시로 끝났다 out.append(f"{start} [{''.join(parts)}]") out.append(f"{physical} {len(out)}") print("\n".join(out))main()
JavaScriptmain.js · 25 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");lines.pop(); // 마지막 줄바꿈 뒤에 생기는 빈 문자열은 줄이 아니다const out = [];let start = 0;let text = "";let open = false; // 앞줄이 역슬래시로 끝나 아직 이어지는 중인가for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (!open) { start = i + 1; text = ""; } if (line.endsWith("\\")) { text += line.slice(0, -1); // 마지막 역슬래시 하나만 뗀다 open = true; } else { text += line; out.push(`${start} [${text}]`); open = false; }}if (open) out.push(`${start} [${text}]`); // 마지막 줄이 역슬래시로 끝났다out.push(`${lines.length} ${out.length}`);console.log(out.join("\n"));
TypeScriptmain.ts · 26 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");lines.pop(); // 마지막 줄바꿈 뒤에 생기는 빈 문자열은 줄이 아니다const out: string[] = [];let start = 0;let text = "";let open = false; // 앞줄이 역슬래시로 끝나 아직 이어지는 중인가lines.forEach((line: string, i: number) => { if (!open) { start = i + 1; text = ""; } if (line.endsWith("\\")) { text += line.slice(0, -1); // 마지막 역슬래시 하나만 뗀다 open = true; } else { text += line; out.push(`${start} [${text}]`); open = false; }});if (open) out.push(`${start} [${text}]`); // 마지막 줄이 역슬래시로 끝났다out.push(`${lines.length} ${out.length}`);console.log(out.join("\n"));
JavaMain.java · 34 行
import java.io.*;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder out = new StringBuilder(); StringBuilder text = new StringBuilder(); int physical = 0, logical = 0, start = 0; boolean open = false; // 앞줄이 역슬래시로 끝나 아직 이어지는 중인가 String line; while ((line = br.readLine()) != null) { // 입력이 끝나면 null을 돌려준다 physical++; if (!open) { start = physical; text.setLength(0); } if (line.endsWith("\\")) { text.append(line, 0, line.length() - 1); // 마지막 역슬래시 하나만 뗀다 open = true; } else { text.append(line); out.append(start).append(" [").append(text).append("]\n"); logical++; open = false; } } if (open) { // 마지막 줄이 역슬래시로 끝났다 out.append(start).append(" [").append(text).append("]\n"); logical++; } out.append(physical).append(' ').append(logical).append('\n'); System.out.print(out); }}
C#Main.cs · 42 行
using System;using System.Text;class Program{ static void Main() { var output = new StringBuilder(); var text = new StringBuilder(); int physical = 0, logical = 0, start = 0; bool open = false; // 앞줄이 역슬래시로 끝나 아직 이어지는 중인가 string? line; while ((line = Console.ReadLine()) != null) // 입력이 끝나면 null을 돌려준다 { physical++; if (!open) { start = physical; text.Clear(); } if (line.EndsWith('\\')) { text.Append(line, 0, line.Length - 1); // 마지막 역슬래시 하나만 뗀다 open = true; } else { text.Append(line); output.Append(start).Append(" [").Append(text).Append("]\n"); logical++; open = false; } } if (open) // 마지막 줄이 역슬래시로 끝났다 { output.Append(start).Append(" [").Append(text).Append("]\n"); logical++; } output.Append(physical).Append(' ').Append(logical).Append('\n'); Console.Write(output); }}
C++main.cpp · 31 行
#include <iostream>#include <string>int main() { std::ios::sync_with_stdio(false); std::string line, text; int physical = 0, logical = 0, start = 0; bool open = false; // 앞줄이 역슬래시로 끝나 아직 이어지는 중인가 while (std::getline(std::cin, line)) { // 입력이 끝나면 거짓이 된다. 줄바꿈은 떼어 준다 physical++; if (!open) { start = physical; text.clear(); } if (!line.empty() && line.back() == '\\') { line.pop_back(); // 마지막 역슬래시 하나만 뗀다 text += line; open = true; } else { text += line; std::cout << start << " [" << text << "]\n"; logical++; open = false; } } if (open) { // 마지막 줄이 역슬래시로 끝났다 std::cout << start << " [" << text << "]\n"; logical++; } std::cout << physical << ' ' << logical << '\n';}
Cmain.c · 31 行
#include <stdbool.h>#include <stdio.h>#include <string.h>int main(void) { char line[128]; // 100자 + 줄바꿈 + 끝 표시('\0')가 들어가야 한다 int physical = 0, logical = 0; bool open = false; // 앞줄이 역슬래시로 끝나 아직 이어지는 중인가 while (fgets(line, sizeof line, stdin) != NULL) { // 입력이 끝나면 NULL을 돌려준다 size_t len = strlen(line); if (len > 0 && line[len - 1] == '\n') line[--len] = '\0'; // fgets는 줄바꿈까지 담아 준다 physical++; if (!open) { // 이은 줄을 모아 두지 않고, 읽는 대로 이어서 출력한다 printf("%d [", physical); logical++; } if (len > 0 && line[len - 1] == '\\') { line[len - 1] = '\0'; // 마지막 역슬래시 하나만 뗀다 fputs(line, stdout); open = true; } else { fputs(line, stdout); puts("]"); open = false; } } if (open) puts("]"); // 마지막 줄이 역슬래시로 끝났다 printf("%d %d\n", physical, logical); return 0;}
Craykonmain.cray · 37 行
physical = 0;logical = 0;start = 0;text = "";open = 假; // 앞줄이 역슬래시로 끝나 아직 이어지는 중인가当 (真){ line = 控制台.读行(); 如果 (line == 空) { 跳出; } physical += 1; 如果 (!open) { start = physical; text = ""; } 如果 (line.结尾是("\\")) { text += line.截取(0, line.长度 - 1); // 마지막 역슬래시 하나만 뗀다 open = 真; } 否则 { text += line; 控制台.输出(start + " [" + text + "]"); logical += 1; open = 假; }}如果 (open){ 控制台.输出(start + " [" + text + "]"); logical += 1;}控制台.输出(physical + " " + logical);