import syslines = sys.stdin.read().split("\n")q = int(lines[0])scores = {}out = []for line in lines[1:q + 1]: name, delta = line.split() scores[name] = scores.get(name, 0) + int(delta) # 처음 나온 이름은 0점부터 # 점수가 높은 사람 먼저, 같으면 이름 사전 순 top = sorted(scores.items(), key=lambda kv: (-kv[1], kv[0]))[:3] out.append(" ".join(f"{n}:{s}" for n, s in top))print("\n".join(out))
JavaScriptmain.js · 13 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const q = Number(lines[0]);const scores = new Map();const out = [];for (const line of lines.slice(1, q + 1)) { const [name, delta] = line.trim().split(" "); scores.set(name, (scores.get(name) ?? 0) + Number(delta)); // 없으면 undefined + 수 = NaN const top = [...scores.entries()] .sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1)) // 점수 높은 사람 먼저, 같으면 이름 순 .slice(0, 3); out.push(top.map(([n, s]) => `${n}:${s}`).join(" "));}console.log(out.join("\n"));
TypeScriptmain.ts · 14 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const q = Number(lines[0]);const scores = new Map<string, number>();const out: string[] = [];for (const line of lines.slice(1, q + 1)) { const [name, delta] = line.trim().split(" "); scores.set(name, (scores.get(name) ?? 0) + Number(delta)); // 없으면 undefined + 수 = NaN const top: [string, number][] = [...scores.entries()] .sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1)) // 점수 높은 사람 먼저, 같으면 이름 순 .slice(0, 3); out.push(top.map(([n, s]) => `${n}:${s}`).join(" "));}console.log(out.join("\n"));
JavaMain.java · 25 行
import java.io.*;import java.util.*;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(br.readLine().trim()); Map<String, Integer> scores = new HashMap<>(); StringBuilder out = new StringBuilder(); Comparator<Map.Entry<String, Integer>> order = Map.Entry.<String, Integer>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()); for (int i = 0; i < q; i++) { String[] p = br.readLine().trim().split(" "); scores.merge(p[0], Integer.parseInt(p[1]), Integer::sum); // 없으면 그 값을, 있으면 더한 값을 넣는다 List<Map.Entry<String, Integer>> all = new ArrayList<>(scores.entrySet()); all.sort(order); StringJoiner sj = new StringJoiner(" "); for (int j = 0; j < Math.min(3, all.size()); j++) sj.add(all.get(j).getKey() + ":" + all.get(j).getValue()); out.append(sj).append('\n'); } System.out.print(out); }}
C#Main.cs · 28 行
using System;using System.Collections.Generic;using System.Linq;using System.Text;class Program{ static void Main() { int q = int.Parse(Console.ReadLine().Trim()); var scores = new Dictionary<string, int>(); var sb = new StringBuilder(); for (int i = 0; i < q; i++) { string[] p = Console.ReadLine().Trim().Split(' '); scores.TryGetValue(p[0], out int now); // 없으면 now = 0 scores[p[0]] = now + int.Parse(p[1]); var top = scores .OrderByDescending(kv => kv.Value) .ThenBy(kv => kv.Key, StringComparer.Ordinal) .Take(3) .Select(kv => $"{kv.Key}:{kv.Value}"); sb.Append(string.Join(" ", top)).Append('\n'); } Console.Write(sb); }}
C++main.cpp · 29 行
#include <algorithm>#include <iostream>#include <map>#include <string>#include <vector>int main() { int q; std::cin >> q; std::map<std::string, int> scores; std::string out; for (int i = 0; i < q; i++) { std::string name; int delta; std::cin >> name >> delta; scores[name] += delta; // 없는 열쇠는 []가 0으로 만들어 준다 std::vector<std::pair<std::string, int>> all(scores.begin(), scores.end()); // 셋만 필요하니 partial_sort로 앞 세 자리만 맞춘다 size_t k = std::min<size_t>(3, all.size()); std::partial_sort(all.begin(), all.begin() + k, all.end(), [](const auto& a, const auto& b) { if (a.second != b.second) return a.second > b.second; return a.first < b.first; }); for (size_t j = 0; j < k; j++) out += (j ? " " : "") + all[j].first + ":" + std::to_string(all[j].second); out += "\n"; } std::cout << out;}
Cmain.c · 46 行
#include <stdio.h>#include <string.h>/* C에는 사전이 없다: 이름은 많아야 100개라 이름 표를 차례로 찾는다 */static char names[100][11];static int score[100];static int count = 0;/* i가 j보다 앞서야 하나: 점수가 높거나, 같으면 이름이 사전 순으로 앞선다 */static int before(int i, int j) { if (score[i] != score[j]) return score[i] > score[j]; return strcmp(names[i], names[j]) < 0;}int main(void) { int q; scanf("%d", &q); for (int e = 0; e < q; e++) { char name[11]; int delta, at = -1; scanf("%10s %d", name, &delta); for (int i = 0; i < count; i++) if (strcmp(names[i], name) == 0) { at = i; break; } if (at < 0) { /* 처음 나온 이름은 0점부터 */ at = count++; strcpy(names[at], name); score[at] = 0; } score[at] += delta; /* 세 번 돌며 아직 뽑지 않은 사람 중 가장 앞서는 사람을 고른다 */ int top[3], k = count < 3 ? count : 3; for (int t = 0; t < k; t++) { int best = -1; for (int i = 0; i < count; i++) { int used = 0; for (int u = 0; u < t; u++) used |= top[u] == i; if (!used && (best < 0 || before(i, best))) best = i; } top[t] = best; printf(t ? " %s:%d" : "%s:%d", names[best], score[best]); } printf("\n"); } return 0;}
Craykonmain.cray · 35 行
q = 控制台.读行().转数字();scores = 字典();循环 (e = 0; e < q; e++){ p = 控制台.读行().去空白().分割(); name = p[0]; 如果 (!scores.包含(name)) { scores[name] = 0; // 없는 열쇠를 읽으면 멈추니 먼저 0점으로 넣는다 } scores[name] = scores[name] + p[1].转数字(); names = scores.键列表; names.排序(앞선사람먼저); top = []; 循环 (i = 0; i < names.数量 && i < 3; i++) { top.添加(names[i] + ":" + scores[names[i]]); } 控制台.输出(top.连接(" "));}// 점수가 높은 사람 먼저, 같으면 이름 사전 순앞선사람먼저(앞, 뒤){ 如果 (scores[앞] != scores[뒤]) { 返回 scores[뒤] - scores[앞]; } 如果 (앞 < 뒤) { 返回 -1; } 返回 1;}