You are typing an exhibition guestbook into a computer.
The first line gives the visitor number n.
The second line gives the visitor's name, and the third line gives the message the visitor left.
The name and the message may contain spaces.
The message may also contain two or more spaces in a row.
Print one line in the form "#n [name] message".
That is, write #, n, one space, [, the name, ], one space and the message, in this order.
Write the name and the message exactly as they appear in the input, down to the number of spaces.
Input
1 ≤ n ≤ 9,999 (given as digits only, without a comma and without leading zeros). The name is 1 to 20 characters long, made of English letters and spaces, and never has two spaces in a row. The message is 1 to 60 characters long, made of English letters, digits, spaces and punctuation (. , ! ?). Neither the name nor the message starts or ends with a space
Output
One line "#n [name] message"
Example 1 · stdin
27
Kim Min Su
So quiet, I loved it!
stdout
#27 [Kim Min Su] So quiet, I loved it!
Example 2 · stdin
3
Joy
Wow
stdout
#3 [Joy] Wow
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
How you read a whole line that contains spaces differs by language. Python input(), Java BufferedReader.readLine() / Scanner.nextLine(), C# Console.ReadLine(), C++ std::getline(std::cin, s) and Craykon Console.ReadLine() read one line and give it to you without the newline at its end. In JS and TS you read all the input at once and split it into lines with split("\n"). Only C fgets keeps the newline in the buffer, so you have to remove it yourself (s[strcspn(s, "\n")] = '\0'). If you don't, the ] in example 1 moves to the next line. The first trap is reading word by word. C++ cin >> name and C scanf("%s") stop at a space, so the name in example 1 becomes Kim and Min lands where the message should be. The second trap is reading a line right after reading a number. C++ cin >> n, Java Scanner.nextInt() and C scanf("%d") read only the number and leave the newline that ends the first line. The getline, nextLine() or fgets that follows reads that leftover empty line, and example 1 becomes #27 [] Kim Min Su. Read the number as a line and convert it (Java Integer.parseInt(br.readLine()), C fgets then atoi), or in C++ throw the leftover newline away with cin.ignore. The third trap is splitting and joining again. Python " ".join(input().split()) shrinks the two spaces between So and quiet into one.
Pythonmain.py · 4 lines
n = int(input())name = input() # input()은 한 줄을 통째로 읽고 줄 바꿈은 떼어 준다msg = input()print(f"#{n} [{name}] {msg}")
JavaScriptmain.js · 6 lines
// 입력을 한꺼번에 읽어 줄 바꿈으로 나누면 줄마다 빈칸이 그대로 남는다const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const name = lines[1];const msg = lines[2];console.log(`#${n} [${name}] ${msg}`);
TypeScriptmain.ts · 7 lines
import { readFileSync } from "node:fs";// 입력을 한꺼번에 읽어 줄 바꿈으로 나누면 줄마다 빈칸이 그대로 남는다const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const name: string = lines[1];const msg: string = lines[2];console.log(`#${n} [${name}] ${msg}`);
JavaMain.java · 13 lines
import java.io.BufferedReader;import java.io.InputStreamReader;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()); String name = br.readLine(); String msg = br.readLine(); System.out.println("#" + n + " [" + name + "] " + msg); }}
C#Main.cs · 12 lines
using System;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); string name = Console.ReadLine(); // 한 줄을 통째로, 줄 바꿈은 빼고 string msg = Console.ReadLine(); Console.WriteLine($"#{n} [{name}] {msg}"); }}
C++main.cpp · 14 lines
#include <iostream>#include <limits>#include <string>int main() { int n; std::cin >> n; // cin >> n은 수만 읽는다. 첫 줄에 남은 줄 바꿈을 버려야 다음 getline이 이름을 읽는다 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::string name, msg; std::getline(std::cin, name); std::getline(std::cin, msg); std::cout << "#" << n << " [" << name << "] " << msg << "\n";}
Cmain.c · 15 lines
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(void) { char num[16], name[32], msg[80]; if (!fgets(num, sizeof num, stdin)) return 0; if (!fgets(name, sizeof name, stdin)) return 0; if (!fgets(msg, sizeof msg, stdin)) return 0; /* fgets는 줄 바꿈까지 담아 주므로 직접 지운다 */ name[strcspn(name, "\n")] = '\0'; msg[strcspn(msg, "\n")] = '\0'; printf("#%d [%s] %s\n", atoi(num), name, msg); return 0;}
Craykonmain.cray · 5 lines
n = Console.ReadLine().ToNumber();// 줄읽기는 한 줄을 통째로 준다(줄 바꿈은 빼고)name = Console.ReadLine();msg = Console.ReadLine();Console.WriteLine("#" + n + " [" + name + "] " + msg);