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]); } でなければ { コンソール.書く("なし"); }}