The first line gives the number of records n, and each of the next n lines gives "number name" separated by one space. If the same number appears more than once, only the name that appears last is kept. For each distinct number print "number name" on its own line, from the smallest number up (by numeric value, negatives first).
Input
1 ≤ n ≤ 2000, -1,000,000,000 ≤ number ≤ 1,000,000,000 (integer). Names are 1 to 12 lowercase English letters
Output
one line per distinct number: the number and the name separated by one space
Example 1 · stdin
5
30 mina
-2 jun
100 seo
30 hana
7 ari
stdout
-2 jun
7 ari
30 hana
100 seo
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
The order you get when iterating a map differs by language. Python dict, JS Map and C# Dictionary (not guaranteed by the docs) give insertion order; Java HashMap and C++ unordered_map have no defined order. The ones kept sorted by key are C++ std::map, Java TreeMap and C# SortedDictionary. Python iterates sorted(roster). JS has two traps: keys.sort() without a compare function compares numbers as strings, so 10 comes before 9 (pass (a, b) => a - b), and a plain object {} puts only non-negative integer keys first in ascending order, with keys like "-3" after them in insertion order. Keeping the number as a string key gives string order in any language. C has no map, so it qsorts an array of structs by (number, arrival order) and keeps the last of each number. Craykon's Dict() keeps insertion order like Python, so sort with keys = roster.Keys; keys.Sort(); (numbers by size).
Pythonmain.py · 10 lines
import syslines = sys.stdin.read().split("\n")n = int(lines[0])roster = {}for line in lines[1:n + 1]: key, name = line.split() roster[int(key)] = namefor key in sorted(roster): print(key, roster[key])
JavaScriptmain.js · 9 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const roster = new Map();for (let i = 1; i <= n; i++) { const [key, name] = lines[i].trim().split(" "); roster.set(Number(key), name);}const keys = [...roster.keys()].sort((a, b) => a - b);console.log(keys.map((k) => `${k} ${roster.get(k)}`).join("\n"));
TypeScriptmain.ts · 10 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const roster = new Map<number, string>();for (let i = 1; i <= n; i++) { const [key, name] = lines[i].trim().split(" "); roster.set(Number(key), name);}const keys: number[] = [...roster.keys()].sort((a, b) => a - b);console.log(keys.map((k) => `${k} ${roster.get(k)}`).join("\n"));
JavaMain.java · 21 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Map;import java.util.TreeMap;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); Map<Integer, String> roster = new TreeMap<>(); for (int i = 0; i < n; i++) { String[] p = in.readLine().trim().split(" "); roster.put(Integer.parseInt(p[0]), p[1]); } StringBuilder out = new StringBuilder(); for (Map.Entry<Integer, String> e : roster.entrySet()) out.append(e.getKey()).append(' ').append(e.getValue()).append('\n'); System.out.print(out); }}
C#Main.cs · 17 lines
using System;using System.Collections.Generic;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); var roster = new SortedDictionary<int, string>(); for (int i = 0; i < n; i++) { string[] p = Console.ReadLine().Trim().Split(' '); roster[int.Parse(p[0])] = p[1]; } foreach (var kv in roster) Console.WriteLine($"{kv.Key} {kv.Value}"); }}
C++main.cpp · 16 lines
#include <iostream>#include <map>#include <string>int main() { int n; std::cin >> n; std::map<int, std::string> roster; // std::map은 열쇠 순으로 정렬된 채 있다 for (int i = 0; i < n; i++) { int key; std::string name; std::cin >> key >> name; roster[key] = name; } for (const auto& [key, name] : roster) std::cout << key << ' ' << name << '\n';}
Cmain.c · 29 lines
#include <stdio.h>#include <stdlib.h>struct Entry { int key; int order; /* 몇째로 들어왔나: 같은 번호면 나중 것이 이긴다 */ char name[13];};static int by_key_then_order(const void *pa, const void *pb) { const struct Entry *a = pa, *b = pb; if (a->key != b->key) return a->key < b->key ? -1 : 1; return a->order - b->order;}int main(void) { static struct Entry e[2000]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %12s", &e[i].key, e[i].name); e[i].order = i; } /* C에는 사전이 없다: 번호 순으로 정렬하고, 같은 번호 묶음의 마지막 것만 쓴다 */ qsort(e, n, sizeof e[0], by_key_then_order); for (int i = 0; i < n; i++) if (i == n - 1 || e[i + 1].key != e[i].key) printf("%d %s\n", e[i].key, e[i].name); return 0;}
Craykonmain.cray · 13 lines
n = Console.ReadLine().ToNumber();roster = Dict();for (i = 0; i < n; i++){ p = Console.ReadLine().Trim().Split(); roster[p[0].ToNumber()] = p[1];}keys = roster.Keys;keys.Sort();foreach (k in keys){ Console.WriteLine(k + " " + roster[k]);}