import sysrows = count_all = total = 0out = []for line in sys.stdin: # 입력이 끝날 때까지 한 줄씩 data = line.split("#")[0] # '#'부터는 주석 # 쉼표를 빈칸으로 바꾸면 split()이 빈칸·탭·쉼표 덩이를 한 번에 건너뛴다 nums = [int(t) for t in data.replace(",", " ").split()] if not nums: # 수가 없는 줄은 기록이 아니다 continue rows += 1 count_all += len(nums) total += sum(nums) out.append(f"{len(nums)} {sum(nums)}")out.append(f"{rows} {count_all} {total}")print("\n".join(out))
JavaScriptmain.js · 19 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");let rows = 0;let countAll = 0;let total = 0;const out = [];for (const line of lines) { const data = line.split("#")[0]; // '#'부터는 주석 // 나눈 뒤 빈 조각 ""을 꼭 걸러 낸다. Number("")는 오류 없이 0이다 const nums = data.split(/[ \t,]+/).filter((t) => t !== "").map(Number); if (nums.length === 0) continue; // 수가 없는 줄은 기록이 아니다 let sum = 0; for (const x of nums) sum += x; rows++; countAll += nums.length; total += sum; out.push(`${nums.length} ${sum}`);}out.push(`${rows} ${countAll} ${total}`);console.log(out.join("\n"));
TypeScriptmain.ts · 20 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");let rows = 0;let countAll = 0;let total = 0;const out: string[] = [];for (const line of lines) { const data: string = line.split("#")[0]; // '#'부터는 주석 // 나눈 뒤 빈 조각 ""을 꼭 걸러 낸다. Number("")는 오류 없이 0이다 const nums: number[] = data.split(/[ \t,]+/).filter((t) => t !== "").map(Number); if (nums.length === 0) continue; // 수가 없는 줄은 기록이 아니다 let sum = 0; for (const x of nums) sum += x; rows++; countAll += nums.length; total += sum; out.push(`${nums.length} ${sum}`);}out.push(`${rows} ${countAll} ${total}`);console.log(out.join("\n"));
JavaMain.java · 33 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int rows = 0; int countAll = 0; long total = 0; StringBuilder out = new StringBuilder(); String line; while ((line = in.readLine()) != null) { // 입력이 끝나면 null int hash = line.indexOf('#'); String data = hash >= 0 ? line.substring(0, hash) : line; // '#'부터는 주석 // StringTokenizer는 구분 글자가 겹치거나 맨 앞에 있어도 빈 조각을 만들지 않는다 StringTokenizer st = new StringTokenizer(data, " \t,"); int count = 0; long sum = 0; while (st.hasMoreTokens()) { sum += Long.parseLong(st.nextToken()); count++; } if (count == 0) continue; // 수가 없는 줄은 기록이 아니다 rows++; countAll += count; total += sum; out.append(count).append(' ').append(sum).append('\n'); } out.append(rows).append(' ').append(countAll).append(' ').append(total).append('\n'); System.out.print(out); }}
C#Main.cs · 29 行
using System;using System.Text;class Program{ static void Main() { int rows = 0, countAll = 0; long total = 0; var sb = new StringBuilder(); char[] seps = { ' ', '\t', ',' }; string line; while ((line = Console.ReadLine()) != null) // 입력이 끝나면 null { string data = line.Split('#')[0]; // '#'부터는 주석 // RemoveEmptyEntries가 없으면 구분 글자가 겹친 곳마다 빈 문자열이 생긴다 string[] parts = data.Split(seps, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 0) continue; // 수가 없는 줄은 기록이 아니다 long sum = 0; foreach (string t in parts) sum += long.Parse(t); rows++; countAll += parts.Length; total += sum; sb.Append(parts.Length).Append(' ').Append(sum).Append('\n'); } sb.Append(rows).Append(' ').Append(countAll).Append(' ').Append(total).Append('\n'); Console.Write(sb); }}
C++main.cpp · 30 行
#include <algorithm>#include <iostream>#include <sstream>#include <string>using namespace std;int main() { int rows = 0, countAll = 0; long long total = 0; string line; // cin >> x 는 줄 바꿈도 건너뛰어서 줄의 끝을 알 수 없다. getline으로 한 줄씩 읽는다 while (getline(cin, line)) { line = line.substr(0, line.find('#')); // '#'부터는 주석(없으면 npos라 줄 전체) replace(line.begin(), line.end(), ',', ' '); istringstream in(line); int count = 0; long long sum = 0, x; while (in >> x) { // 빈칸·탭 덩이를 건너뛰며 수를 하나씩 읽는다 sum += x; count++; } if (count == 0) continue; // 수가 없는 줄은 기록이 아니다 rows++; countAll += count; total += sum; cout << count << ' ' << sum << '\n'; } cout << rows << ' ' << countAll << ' ' << total << '\n'; return 0;}
Cmain.c · 27 行
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void) { char line[256]; int rows = 0, count_all = 0; long long total = 0; /* scanf("%lld")는 줄 바꿈도 건너뛰어서 줄의 끝을 알 수 없다. 한 줄씩 읽어서 나눈다 */ while (fgets(line, sizeof line, stdin)) { char *hash = strchr(line, '#'); /* '#'부터는 주석 */ if (hash) *hash = '\0'; int count = 0; long long sum = 0; for (char *tok = strtok(line, " \t,\r\n"); tok; tok = strtok(NULL, " \t,\r\n")) { sum += strtoll(tok, NULL, 10); /* 꼭 10진법으로: 0을 주면 010을 8진수로 읽는다 */ count++; } if (count == 0) continue; /* 수가 없는 줄은 기록이 아니다 */ rows++; count_all += count; total += sum; printf("%d %lld\n", count, sum); } printf("%d %d %lld\n", rows, count_all, total); return 0;}
Craykonmain.cray · 28 行
rows = 0;countAll = 0;total = 0;間 (真){ line = コンソール.行読み(); もし (line == なし) { 抜ける; } data = line.分ける("#")[0]; // '#'부터는 주석 // 쉼표를 빈칸으로 바꾸면 나누기()가 빈칸·탭·쉼표 덩이를 한 번에 건너뛴다 parts = data.置き換え(",", " ").分ける(); もし (parts.個数 == 0) { 次へ; } sum = 0; それぞれ (t の中 parts) { sum += t.数に(); } rows += 1; countAll += parts.個数; total += sum; コンソール.書く(parts.個数 + " " + sum);}コンソール.書く(rows + " " + countAll + " " + total);