import sysdata = sys.stdin.read().split("\n")n = int(data[0])total = {}for line in data[1:n + 1]: zone, floor, cell, delta = line.split() key = (zone, int(floor), int(cell)) # 07과 7이 같은 열쇠가 되도록 수로 바꿔 묶는다 total[key] = total.get(key, 0) + int(delta)keys = [k for k in total if total[k] != 0] # 합이 0인 칸은 빼고keys.sort(key=lambda k: (k[0], -k[1], k[2])) # 구역 오름차순, 층 내림차순, 칸 오름차순out = [f"{z} {f} {c} {total[(z, f, c)]}" for z, f, c in keys]out.append(f"{len(keys)} {sum(total.values())}")print("\n".join(out))
JavaScriptmain.js · 25 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const total = new Map();for (let i = 1; i <= n; i++) { const [zone, floor, cell, delta] = lines[i].split(" "); // 배열은 열쇠로 쓸 수 없으니(같은 내용이어도 다른 열쇠) 수로 바꾼 뒤 문자열 하나로 만든다 const key = `${zone} ${Number(floor)} ${Number(cell)}`; total.set(key, (total.get(key) ?? 0) + Number(delta));}const rows = [];let grand = 0;for (const [key, sum] of total) { grand += sum; if (sum === 0) continue; // 합이 0인 칸은 뺀다 const [zone, floor, cell] = key.split(" "); rows.push({ zone, floor: Number(floor), cell: Number(cell), sum });}rows.sort((a, b) => { if (a.zone !== b.zone) return a.zone < b.zone ? -1 : 1; // 구역: 글자 코드 순 if (a.floor !== b.floor) return b.floor - a.floor; // 층: 높은 층 먼저 return a.cell - b.cell; // 칸: 작은 번호 먼저});const out = rows.map((r) => `${r.zone} ${r.floor} ${r.cell} ${r.sum}`);out.push(`${rows.length} ${grand}`);console.log(out.join("\n"));
TypeScriptmain.ts · 27 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const total = new Map<string, number>();for (let i = 1; i <= n; i++) { const [zone, floor, cell, delta] = lines[i].split(" "); // 배열은 열쇠로 쓸 수 없으니(같은 내용이어도 다른 열쇠) 수로 바꾼 뒤 문자열 하나로 만든다 const key = `${zone} ${Number(floor)} ${Number(cell)}`; total.set(key, (total.get(key) ?? 0) + Number(delta));}const rows: { zone: string; floor: number; cell: number; sum: number }[] = [];let grand = 0;for (const [key, sum] of total) { grand += sum; if (sum === 0) continue; // 합이 0인 칸은 뺀다 const [zone, floor, cell] = key.split(" "); rows.push({ zone, floor: Number(floor), cell: Number(cell), sum });}rows.sort((a, b) => { if (a.zone !== b.zone) return a.zone < b.zone ? -1 : 1; // 구역: 글자 코드 순 if (a.floor !== b.floor) return b.floor - a.floor; // 층: 높은 층 먼저 return a.cell - b.cell; // 칸: 작은 번호 먼저});const out = rows.map((r) => `${r.zone} ${r.floor} ${r.cell} ${r.sum}`);out.push(`${rows.length} ${grand}`);console.log(out.join("\n"));
JavaMain.java · 41 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Comparator;import java.util.Map;import java.util.StringTokenizer;import java.util.TreeMap;public class Main { record Key(String zone, int floor, int cell) {} public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); // 구역 오름차순, 층 내림차순, 칸 오름차순. 층에만 reverseOrder를 건다 Comparator<Key> order = Comparator.comparing(Key::zone) .thenComparing(Key::floor, Comparator.reverseOrder()) .thenComparingInt(Key::cell); TreeMap<Key, Integer> total = new TreeMap<>(order); for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(in.readLine()); String zone = st.nextToken(); int floor = Integer.parseInt(st.nextToken()); int cell = Integer.parseInt(st.nextToken()); int delta = Integer.parseInt(st.nextToken()); total.merge(new Key(zone, floor, cell), delta, Integer::sum); } StringBuilder out = new StringBuilder(); int count = 0; int grand = 0; for (Map.Entry<Key, Integer> e : total.entrySet()) { int sum = e.getValue(); grand += sum; if (sum == 0) continue; // 합이 0인 칸은 뺀다 Key k = e.getKey(); out.append(k.zone()).append(' ').append(k.floor()).append(' ').append(k.cell()).append(' ').append(sum).append('\n'); count++; } out.append(count).append(' ').append(grand).append('\n'); System.out.print(out); }}
C#Main.cs · 32 行
using System;using System.Collections.Generic;using System.Linq;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()!.Trim()); var total = new Dictionary<(string zone, int floor, int cell), int>(); for (int i = 0; i < n; i++) { string[] p = Console.ReadLine()!.Split(' '); var key = (p[0], int.Parse(p[1]), int.Parse(p[2])); total[key] = total.GetValueOrDefault(key) + int.Parse(p[3]); } // 문자열은 꼭 StringComparer.Ordinal로: 기본 비교는 문화권 순서라 a가 B보다 앞에 온다 var keys = total.Keys.Where(k => total[k] != 0) .OrderBy(k => k.zone, StringComparer.Ordinal) .ThenByDescending(k => k.floor) .ThenBy(k => k.cell) .ToList(); var sb = new StringBuilder(); foreach (var k in keys) { sb.Append(k.zone).Append(' ').Append(k.floor).Append(' ').Append(k.cell).Append(' ').Append(total[k]).Append('\n'); } sb.Append(keys.Count).Append(' ').Append(total.Values.Sum()).Append('\n'); Console.Write(sb); }}
C++main.cpp · 28 行
#include <iostream>#include <map>#include <string>#include <tuple>int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; // std::map은 열쇠를 모두 오름차순으로 둔다. 층만 내림차순이 되도록 부호를 뒤집어 넣는다 std::map<std::tuple<std::string, int, int>, int> total; for (int i = 0; i < n; i++) { std::string zone; int floor, cell, delta; std::cin >> zone >> floor >> cell >> delta; total[{zone, -floor, cell}] += delta; } int count = 0, grand = 0; for (const auto& [key, sum] : total) { grand += sum; if (sum == 0) continue; // 합이 0인 칸은 뺀다 std::cout << std::get<0>(key) << ' ' << -std::get<1>(key) << ' ' << std::get<2>(key) << ' ' << sum << '\n'; count++; } std::cout << count << ' ' << grand << '\n'; return 0;}
Cmain.c · 43 行
#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct { char zone[4]; int floor, cell, delta;} Rec;/* 구역 오름차순, 층 내림차순, 칸 오름차순 */static int cmp(const void *pa, const void *pb) { const Rec *a = pa, *b = pb; int z = strcmp(a->zone, b->zone); if (z != 0) return z; if (a->floor != b->floor) return a->floor > b->floor ? -1 : 1; if (a->cell != b->cell) return a->cell < b->cell ? -1 : 1; return 0;}int main(void) { static Rec r[5000]; int n; if (scanf("%d", &n) != 1) return 0; for (int i = 0; i < n; i++) { /* %d는 앞에 붙은 0과 '+'를 그대로 10진수로 읽는다 */ if (scanf("%3s %d %d %d", r[i].zone, &r[i].floor, &r[i].cell, &r[i].delta) != 4) return 0; } /* 사전이 없으니 정렬해서 같은 칸을 이웃하게 만든 뒤 묶는다 */ qsort(r, (size_t)n, sizeof r[0], cmp); int count = 0, grand = 0; for (int i = 0; i < n;) { int j = i, sum = 0; while (j < n && cmp(&r[i], &r[j]) == 0) sum += r[j++].delta; grand += sum; if (sum != 0) { /* 합이 0인 칸은 뺀다 */ printf("%s %d %d %d\n", r[i].zone, r[i].floor, r[i].cell, sum); count++; } i = j; } printf("%d %d\n", count, grand); return 0;}
Craykonmain.cray · 57 行
n = コンソール.行読み().数に();total = 辞書();parts = 辞書();grand = 0;繰り返す (i = 0; i < n; i++){ p = コンソール.行読み().分ける(); floor = p[1].数に(); cell = p[2].数に(); delta = p[3].数に(); // 07과 7이 같은 열쇠가 되도록 수로 바꾼 뒤 문자열 하나로 만든다 key = p[0] + " " + floor + " " + cell; もし (total.含む(key)) { total[key] += delta; } でなければ { total[key] = delta; parts[key] = [p[0], floor, cell]; } grand += delta;}keys = [];それぞれ (key の中 total.キー一覧){ もし (total[key] != 0) { keys.追加(key); }}keys.並べ替え(차례);それぞれ (key の中 keys){ コンソール.書く(key + " " + total[key]);}コンソール.書く(keys.個数 + " " + grand);// 구역 오름차순, 층 내림차순, 칸 오름차순차례(앞, 뒤){ a = parts[앞]; b = parts[뒤]; もし (a[0] != b[0]) { もし (a[0] < b[0]) { 返す -1; } 返す 1; } もし (a[1] != b[1]) { 返す b[1] - a[1]; } 返す a[2] - b[2];}