n = int(input())total = {}for _ in range(n): code, qty = input().split() total[code] = total.get(code, 0) + int(qty)lo, hi = input().split()# 문자열의 <, sorted는 글자 코드 순이다(숫자 < 대문자 < 소문자)found = [code for code in sorted(total) if lo <= code <= hi]if found: for code in found: print(code, total[code])else: print("无")
JavaScriptmain.js · 11 行
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 [code, qty] = lines[i].trim().split(" "); total.set(code, (total.get(code) ?? 0) + Number(qty));}const [lo, hi] = lines[n + 1].trim().split(" ");// sort()는 기준 없이 쓰면 글자 코드 순이다. localeCompare는 사람이 읽는 순(a < B)이라 쓰지 않는다const found = [...total.keys()].filter((code) => lo <= code && code <= hi).sort();console.log(found.length ? found.map((code) => `${code} ${total.get(code)}`).join("\n") : "无");
TypeScriptmain.ts · 12 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const total = new Map<string, number>();for (let i = 1; i <= n; i++) { const [code, qty]: string[] = lines[i].trim().split(" "); total.set(code, (total.get(code) ?? 0) + Number(qty));}const [lo, hi]: string[] = lines[n + 1].trim().split(" ");// sort()는 기준 없이 쓰면 글자 코드 순이다const found: string[] = [...total.keys()].filter((code) => lo <= code && code <= hi).sort();console.log(found.length ? found.map((code) => `${code} ${total.get(code)}`).join("\n") : "无");
JavaMain.java · 29 行
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Map;import java.util.NavigableMap;import java.util.StringTokenizer;import java.util.TreeMap;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); // TreeMap은 열쇠를 compareTo 순(글자 코드 순)으로 늘 정렬해 둔다 TreeMap<String, Integer> total = new TreeMap<>(); for (int i = 0; i < n; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); total.merge(st.nextToken(), Integer.parseInt(st.nextToken()), Integer::sum); } StringTokenizer st = new StringTokenizer(br.readLine()); String lo = st.nextToken(); String hi = st.nextToken(); // 양 끝을 포함하는 범위 NavigableMap<String, Integer> range = total.subMap(lo, true, hi, true); StringBuilder out = new StringBuilder(); for (Map.Entry<String, Integer> e : range.entrySet()) { out.append(e.getKey()).append(' ').append(e.getValue()).append('\n'); } System.out.print(range.isEmpty() ? "无\n" : out); }}
C#Main.cs · 28 行
using System;using System.Collections.Generic;using System.Text;class Program{ static void Main() { int n = int.Parse(Console.ReadLine().Trim()); // 문자열 비교는 문화권 규칙(a < B)이 기본이라, 글자 코드 순인 Ordinal을 꼭 준다 var total = new SortedDictionary<string, int>(StringComparer.Ordinal); for (int i = 0; i < n; i++) { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); total.TryGetValue(p[0], out int t); total[p[0]] = t + int.Parse(p[1]); } string[] r = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); string lo = r[0], hi = r[1]; var sb = new StringBuilder(); foreach (var e in total) { if (string.CompareOrdinal(e.Key, lo) >= 0 && string.CompareOrdinal(e.Key, hi) <= 0) sb.Append(e.Key).Append(' ').Append(e.Value).Append('\n'); } Console.Write(sb.Length > 0 ? sb.ToString() : "无\n"); }}
C++main.cpp · 24 行
#include <iostream>#include <map>#include <string>int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; // std::map은 열쇠를 글자 코드 순으로 정렬해 둔다 std::map<std::string, int> total; for (int i = 0; i < n; i++) { std::string code; int qty; std::cin >> code >> qty; total[code] += qty; } std::string lo, hi; std::cin >> lo >> hi; // lower_bound(lo): lo 이상인 첫 열쇠, upper_bound(hi): hi보다 큰 첫 열쇠 auto first = total.lower_bound(lo); auto last = total.upper_bound(hi); if (first == last) std::cout << "无\n"; for (auto it = first; it != last; ++it) std::cout << it->first << ' ' << it->second << '\n';}
Cmain.c · 45 行
#include <stdio.h>#include <stdlib.h>#include <string.h>/* C에는 사전이 없다. 코드와 합계를 배열에 모으고, strcmp(글자 코드 순)로 정렬한다 */typedef struct { char code[8]; int total;} Item;static Item items[5000];static int by_code(const void *a, const void *b) { return strcmp(((const Item *)a)->code, ((const Item *)b)->code);}int main(void) { int n, m = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { char code[16]; int qty; scanf("%15s %d", code, &qty); int j = 0; while (j < m && strcmp(items[j].code, code) != 0) j++; if (j == m) { strcpy(items[m].code, code); items[m].total = 0; m++; } items[j].total += qty; } char lo[16], hi[16]; scanf("%15s %15s", lo, hi); qsort(items, m, sizeof(Item), by_code); int shown = 0; for (int j = 0; j < m; j++) { if (strcmp(items[j].code, lo) >= 0 && strcmp(items[j].code, hi) <= 0) { printf("%s %d\n", items[j].code, items[j].total); shown++; } } if (shown == 0) puts("无"); return 0;}
Craykonmain.cray · 30 行
n = 控制台.读行().转数字();total = 字典();循环 (i = 0; i < n; i++){ p = 控制台.读行().去空白().分割(" "); 如果 (!total.包含(p[0])) { total[p[0]] = 0; } total[p[0]] = total[p[0]] + p[1].转数字();}r = 控制台.读行().去空白().分割(" ");lo = r[0];hi = r[1];// 열쇠목록은 넣은 차례다. 정렬()은 문자열을 글자 코드 순으로 늘어놓는다codes = total.键列表;codes.排序();shown = 0;遍历 (code 在 codes){ 如果 (lo <= code && code <= hi) { 控制台.输出(code + " " + total[code]); shown++; }}如果 (shown == 0){ 控制台.输出("无");}