첫 줄에 전화번호부에 실린 사람 수 n이 주어진다. 다음 n줄에는 "이름 번호"가 빈칸 하나로 나뉘어 주어진다. 그다음 줄에 질문 수 q가, 다음 q줄에 찾을 이름이 하나씩 주어진다. 질문마다 그 이름의 번호를 한 줄에 출력하고, 전화번호부에 없는 이름이면 "없음"을 출력하라.
입력
1 ≤ n ≤ 1000, 1 ≤ q ≤ 1000. 이름은 영어 소문자 1 ~ 12글자이고 전화번호부 안에서 서로 다르다. 번호는 숫자와 '-'로 된 1 ~ 20글자(빈칸 없음). 질문하는 이름도 영어 소문자 1 ~ 12글자다
출력
q줄, 줄마다 번호 또는 "없음"
예시 1 · stdin
3
kim 010-1234-5678
lee 02-555-0101
park 031-777-8888
3
lee
choi
kim
stdout
02-555-0101
없음
010-1234-5678
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
함정은 없는 이름을 찾을 때예요. 언어마다 반응이 달라요: Python book[name]은 KeyError로 멈추고(book.get(name, "unknown")), C# book[name]은 KeyNotFoundException을 던지고(TryGetValue), Java get은 조용히 null을 돌려줘서 그대로 붙이면 "null"이 찍혀요(getOrDefault). JS는 빈 객체 {}에서 찾으면 undefined가 나오고, 더 고약하게 "constructor" in book이 참이라 Object.prototype의 함수가 나와요 — Map과 has/get을 쓰세요. C++ book[name]은 없으면 빈 값을 새로 넣어 버리니 find로 물어요. C에는 사전이 없어 이름 순으로 qsort한 뒤 bsearch로 찾아요. 크레이콘은 사전()에 book.있나(name)으로 먼저 묻고 읽어요(없는 열쇠를 읽으면 Python처럼 멈춰요).
Pythonmain.py · 11 줄
import syslines = sys.stdin.read().split("\n")n = int(lines[0])book = {}for line in lines[1:n + 1]: name, number = line.split() book[name] = numberq = int(lines[n + 1])for name in lines[n + 2:n + 2 + q]: print(book.get(name, "없음"))
JavaScriptmain.js · 14 줄
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const book = new Map();for (let i = 1; i <= n; i++) { const [name, number] = lines[i].split(" "); book.set(name, number);}const q = Number(lines[n + 1]);const out = [];for (let i = 0; i < q; i++) { const name = lines[n + 2 + i].trim(); out.push(book.has(name) ? book.get(name) : "없음");}console.log(out.join("\n"));
TypeScriptmain.ts · 15 줄
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const book = new Map<string, string>();for (let i = 1; i <= n; i++) { const [name, number] = lines[i].split(" "); book.set(name, number);}const q: number = Number(lines[n + 1]);const out: string[] = [];for (let i = 0; i < q; i++) { const name: string = lines[n + 2 + i].trim(); out.push(book.get(name) ?? "없음");}console.log(out.join("\n"));
JavaMain.java · 24 줄
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;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<String, String> book = new HashMap<>(); for (int i = 0; i < n; i++) { String[] parts = in.readLine().trim().split(" "); book.put(parts[0], parts[1]); } int q = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < q; i++) { String name = in.readLine().trim(); out.append(book.getOrDefault(name, "없음")).append('\n'); } System.out.print(out); }}
C#Main.cs · 22 줄
using System;using System.Collections.Generic;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); var book = new Dictionary<string, string>(); for (int i = 0; i < n; i++) { string[] parts = Console.ReadLine().Split(' '); book[parts[0]] = parts[1]; } int q = int.Parse(Console.ReadLine()); for (int i = 0; i < q; i++) { string name = Console.ReadLine().Trim(); Console.WriteLine(book.TryGetValue(name, out string number) ? number : "없음"); } }}
C++main.cpp · 21 줄
#include <iostream>#include <string>#include <unordered_map>int main() { int n, q; std::cin >> n; std::unordered_map<std::string, std::string> book; for (int i = 0; i < n; i++) { std::string name, number; std::cin >> name >> number; book[name] = number; } std::cin >> q; for (int i = 0; i < q; i++) { std::string name; std::cin >> name; auto it = book.find(name); std::cout << (it != book.end() ? it->second : "없음") << '\n'; }}
Cmain.c · 31 줄
#include <stdio.h>#include <stdlib.h>#include <string.h>struct Entry { char name[13]; char number[32];};static int by_name(const void *pa, const void *pb) { const struct Entry *a = pa, *b = pb; return strcmp(a->name, b->name);}int main(void) { static struct Entry book[1000]; int n, q; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%12s %31s", book[i].name, book[i].number); /* C에는 사전이 없다: 이름 순으로 정렬해 두고 bsearch로 찾는다 */ qsort(book, n, sizeof book[0], by_name); scanf("%d", &q); for (int i = 0; i < q; i++) { struct Entry key; scanf("%12s", key.name); struct Entry *found = bsearch(&key, book, n, sizeof book[0], by_name); if (found) puts(found->number); else puts("없음"); } return 0;}
Craykonmain.cray · 20 줄
n = 콘솔.줄읽기().수로변환();book = 사전();반복 (i = 0; i < n; i++){ parts = 콘솔.줄읽기().나누기(); book[parts[0]] = parts[1];}q = 콘솔.줄읽기().수로변환();반복 (i = 0; i < q; i++){ name = 콘솔.줄읽기().공백제거(); 만약 (book.있나(name)) { 콘솔.쓰기(book[name]); } 아니면 { 콘솔.쓰기("없음"); }}