import syslines = 0counts = [] # 문단마다 단어 수words = 0 # 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)for raw in sys.stdin: # 입력이 끝나면 반복이 저절로 멈춘다 line = raw.rstrip("\n") # sys.stdin의 줄에는 줄 바꿈이 붙어 있다 lines += 1 if line == "": if words > 0: # 빈 줄이 문단을 닫는다 counts.append(words) words = 0 else: words += len(line.split())if words > 0: # 입력이 빈 줄 없이 끝나면 마지막 문단을 여기서 닫는다 counts.append(words)print(lines, len(counts))print(" ".join(map(str, counts)) if counts else "-")
JavaScriptmain.js · 20 行
// 입력을 통째로 읽어 줄 바꿈으로 나눈다const lines = require("fs").readFileSync(0, "utf8").split("\n");lines.pop(); // 모든 줄이 줄 바꿈으로 끝나므로, 마지막 줄 바꿈 뒤에 생긴 빈 문자열을 버린다const counts = [];let words = 0; // 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)for (const line of lines) { if (line === "") { if (words > 0) { counts.push(words); words = 0; } } else { words += line.split(" ").length; }}if (words > 0) counts.push(words); // 빈 줄 없이 끝난 마지막 문단console.log(`${lines.length} ${counts.length}`);console.log(counts.length > 0 ? counts.join(" ") : "-");
TypeScriptmain.ts · 22 行
import { readFileSync } from "node:fs";// 입력을 통째로 읽어 줄 바꿈으로 나눈다const lines: string[] = readFileSync(0, "utf8").split("\n");lines.pop(); // 모든 줄이 줄 바꿈으로 끝나므로, 마지막 줄 바꿈 뒤에 생긴 빈 문자열을 버린다const counts: number[] = [];let words = 0; // 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)for (const line of lines) { if (line === "") { if (words > 0) { counts.push(words); words = 0; } } else { words += line.split(" ").length; }}if (words > 0) counts.push(words); // 빈 줄 없이 끝난 마지막 문단console.log(`${lines.length} ${counts.length}`);console.log(counts.length > 0 ? counts.join(" ") : "-");
JavaMain.java · 37 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.StringJoiner;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); List<Integer> counts = new ArrayList<>(); int lines = 0, words = 0; String line; // 빈 줄은 ""로, 입력이 끝나면 null이 온다 while ((line = br.readLine()) != null) { lines++; if (line.isEmpty()) { if (words > 0) { counts.add(words); words = 0; } } else { words += line.split(" ").length; } } if (words > 0) counts.add(words); // 빈 줄 없이 끝난 마지막 문단 System.out.println(lines + " " + counts.size()); if (counts.isEmpty()) { System.out.println("-"); } else { StringJoiner sj = new StringJoiner(" "); for (int c : counts) sj.add(String.valueOf(c)); System.out.println(sj); } }}
C#Main.cs · 33 行
using System;using System.Collections.Generic;class Program{ static void Main() { var counts = new List<int>(); int lines = 0, words = 0; string line; // 빈 줄은 ""로, 입력이 끝나면 null이 온다 while ((line = Console.ReadLine()) != null) { lines++; if (line == "") { if (words > 0) { counts.Add(words); words = 0; } } else { words += line.Split(' ').Length; } } if (words > 0) counts.Add(words); // 빈 줄 없이 끝난 마지막 문단 Console.WriteLine($"{lines} {counts.Count}"); Console.WriteLine(counts.Count > 0 ? string.Join(" ", counts) : "-"); }}
C++main.cpp · 38 行
#include <iostream>#include <sstream>#include <string>#include <vector>using namespace std;int main() { string line; vector<int> counts; int lines = 0, words = 0; // getline은 줄 바꿈을 떼어 주고, 입력이 끝나면 거짓이 되어 반복이 멈춘다 while (getline(cin, line)) { lines++; if (line.empty()) { if (words > 0) { counts.push_back(words); words = 0; } } else { istringstream in(line); string w; while (in >> w) words++; } } if (words > 0) counts.push_back(words); // 빈 줄 없이 끝난 마지막 문단 cout << lines << ' ' << counts.size() << '\n'; if (counts.empty()) { cout << "-\n"; } else { for (size_t i = 0; i < counts.size(); i++) { if (i > 0) cout << ' '; cout << counts[i]; } cout << '\n'; } return 0;}
Cmain.c · 36 行
#include <stdio.h>#include <string.h>int main(void) { char buf[128]; static int counts[1000]; /* 문단은 많아야 1,000개 */ int lines = 0, paragraphs = 0, words = 0; /* fgets는 입력이 끝나면 NULL을 준다 */ while (fgets(buf, sizeof buf, stdin)) { buf[strcspn(buf, "\n")] = '\0'; /* fgets가 담아 준 줄 바꿈을 지운다 */ lines++; if (buf[0] == '\0') { if (words > 0) { counts[paragraphs++] = words; words = 0; } } else { words++; /* 단어 수 = 빈칸 수 + 1 */ for (char *p = buf; *p; p++) { if (*p == ' ') words++; } } } if (words > 0) counts[paragraphs++] = words; /* 빈 줄 없이 끝난 마지막 문단 */ printf("%d %d\n", lines, paragraphs); if (paragraphs == 0) { printf("-\n"); } else { for (int i = 0; i < paragraphs; i++) { printf(i == 0 ? "%d" : " %d", counts[i]); } printf("\n"); } return 0;}
Craykonmain.cray · 40 行
줄수 = 0;문단 = []; // 문단마다 단어 수단어 = 0; // 지금 읽고 있는 문단의 단어 수(0이면 문단 밖)줄 = コンソール.行読み(); // 빈 줄은 "", 입력이 끝나면 빈값間 (줄 != なし){ 줄수 += 1; もし (줄 == "") { もし (단어 > 0) { 문단.追加(단어); 단어 = 0; } } でなければ { 단어 += 줄.分ける(" ").個数; } 줄 = コンソール.行読み();}もし (단어 > 0){ 문단.追加(단어); // 빈 줄 없이 끝난 마지막 문단}コンソール.書く(줄수 + " " + 문단.個数);もし (문단.個数 == 0){ コンソール.書く("-");}でなければ{ 결과 = "" + 문단[0]; 繰り返す (i = 1; i < 문단.個数; i++) { 결과 = 결과 + " " + 문단[i]; } コンソール.書く(결과);}