DIGIT = ["", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"]def read_group(n): """1 ~ 9999를 읽는다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천)""" out = "" for unit, size in (("천", 1000), ("백", 100), ("십", 10)): d = n // size % 10 if d == 1: out += unit elif d > 1: out += DIGIT[d] + unit return out + DIGIT[n % 10]def read_number(x): if x == 0: return "영" out = "" for unit, size in (("조", 10 ** 12), ("억", 10 ** 8), ("만", 10 ** 4), ("", 1)): g = x // size % 10000 if g == 0: continue # 빈 묶음은 단위까지 통째로 건너뛴다 if g == 1 and unit == "만": out += "만" # 10000은 "일만"이 아니라 "만" else: out += read_group(g) + unit return outn = int(input())for _ in range(n): print(read_number(int(input())))
JavaScriptmain.js · 31 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const DIGIT = ["", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"];// 1 ~ 9999를 읽는다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천)function readGroup(n) { let out = ""; for (const [unit, size] of [["천", 1000], ["백", 100], ["십", 10]]) { const d = Math.floor(n / size) % 10; if (d === 1) out += unit; else if (d > 1) out += DIGIT[d] + unit; } return out + DIGIT[n % 10];}// 999조까지는 2^53보다 작아서 Number로도 정확하다function readNumber(x) { if (x === 0) return "영"; let out = ""; for (const [unit, size] of [["조", 1e12], ["억", 1e8], ["만", 1e4], ["", 1]]) { const g = Math.floor(x / size) % 10000; if (g === 0) continue; // 빈 묶음은 단위까지 통째로 건너뛴다 if (g === 1 && unit === "만") out += "만"; // 10000은 "일만"이 아니라 "만" else out += readGroup(g) + unit; } return out;}const n = Number(lines[0]);const out = [];for (let i = 1; i <= n; i++) out.push(readNumber(Number(lines[i])));console.log(out.join("\n"));
TypeScriptmain.ts · 35 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const DIGIT: string[] = ["", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"];// 1 ~ 9999를 읽는다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천)function readGroup(n: number): string { let out = ""; const places: [string, number][] = [["천", 1000], ["백", 100], ["십", 10]]; for (const [unit, size] of places) { const d = Math.floor(n / size) % 10; if (d === 1) out += unit; else if (d > 1) out += DIGIT[d] + unit; } return out + DIGIT[n % 10];}// 999조까지는 2^53보다 작아서 number로도 정확하다function readNumber(x: number): string { if (x === 0) return "영"; let out = ""; const groups: [string, number][] = [["조", 1e12], ["억", 1e8], ["만", 1e4], ["", 1]]; for (const [unit, size] of groups) { const g = Math.floor(x / size) % 10000; if (g === 0) continue; // 빈 묶음은 단위까지 통째로 건너뛴다 if (g === 1 && unit === "만") out += "만"; // 10000은 "일만"이 아니라 "만" else out += readGroup(g) + unit; } return out;}const n = Number(lines[0]);const out: string[] = [];for (let i = 1; i <= n; i++) out.push(readNumber(Number(lines[i])));console.log(out.join("\n"));
JavaMain.java · 43 行
import java.io.*;public class Main { static final String[] DIGIT = {"", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"}; static final String[] PLACE = {"천", "백", "십"}; static final int[] PLACE_SIZE = {1000, 100, 10}; static final String[] GROUP = {"조", "억", "만", ""}; static final long[] GROUP_SIZE = {1_000_000_000_000L, 100_000_000L, 10_000L, 1L}; // 1 ~ 9999를 읽는다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천) static String readGroup(int n) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 3; i++) { int d = n / PLACE_SIZE[i] % 10; if (d == 1) sb.append(PLACE[i]); else if (d > 1) sb.append(DIGIT[d]).append(PLACE[i]); } return sb.append(DIGIT[n % 10]).toString(); } // 999조는 int(약 21억)를 넘으니 long으로 받는다 static String readNumber(long x) { if (x == 0) return "영"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; i++) { int g = (int) (x / GROUP_SIZE[i] % 10000); if (g == 0) continue; // 빈 묶음은 단위까지 통째로 건너뛴다 if (g == 1 && GROUP[i].equals("만")) sb.append("만"); // 10000은 "일만"이 아니라 "만" else sb.append(readGroup(g)).append(GROUP[i]); } return sb.toString(); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { out.append(readNumber(Long.parseLong(br.readLine().trim()))).append('\n'); } System.out.print(out); }}
C#Main.cs · 48 行
using System;using System.Text;class Program{ static readonly string[] Digit = { "", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구" }; static readonly (string Unit, int Size)[] Places = { ("천", 1000), ("백", 100), ("십", 10) }; static readonly (string Unit, long Size)[] Groups = { ("조", 1_000_000_000_000L), ("억", 100_000_000L), ("만", 10_000L), ("", 1L) }; // 1 ~ 9999를 읽는다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천) static string ReadGroup(int n) { var sb = new StringBuilder(); foreach (var (unit, size) in Places) { int d = n / size % 10; if (d == 1) sb.Append(unit); else if (d > 1) sb.Append(Digit[d]).Append(unit); } return sb.Append(Digit[n % 10]).ToString(); } // 999조는 int(약 21억)를 넘으니 long으로 받는다 static string ReadNumber(long x) { if (x == 0) return "영"; var sb = new StringBuilder(); foreach (var (unit, size) in Groups) { int g = (int)(x / size % 10000); if (g == 0) continue; // 빈 묶음은 단위까지 통째로 건너뛴다 if (g == 1 && unit == "만") sb.Append("만"); // 10000은 "일만"이 아니라 "만" else sb.Append(ReadGroup(g)).Append(unit); } return sb.ToString(); } static void Main() { int n = int.Parse(Console.ReadLine()!.Trim()); var output = new StringBuilder(); for (int i = 0; i < n; i++) { output.Append(ReadNumber(long.Parse(Console.ReadLine()!.Trim()))).Append('\n'); } Console.Write(output); }}
C++main.cpp · 43 行
#include <array>#include <iostream>#include <string>#include <utility>const std::array<std::string, 10> DIGIT = {"", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"};// 1 ~ 9999를 읽는다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천)std::string readGroup(int n) { const std::array<std::pair<std::string, int>, 3> places = {{{"천", 1000}, {"백", 100}, {"십", 10}}}; std::string out; for (const auto& [unit, size] : places) { int d = n / size % 10; if (d == 1) out += unit; else if (d > 1) out += DIGIT[d] + unit; } return out + DIGIT[n % 10];}// 999조는 int(약 21억)를 넘으니 long long으로 받는다std::string readNumber(long long x) { if (x == 0) return "영"; const std::array<std::pair<std::string, long long>, 4> groups = {{{"조", 1000000000000LL}, {"억", 100000000LL}, {"만", 10000LL}, {"", 1LL}}}; std::string out; for (const auto& [unit, size] : groups) { int g = static_cast<int>(x / size % 10000); if (g == 0) continue; // 빈 묶음은 단위까지 통째로 건너뛴다 if (g == 1 && unit == "만") out += "만"; // 10000은 "일만"이 아니라 "만" else out += readGroup(g) + unit; } return out;}int main() { std::ios::sync_with_stdio(false); int n; long long x; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> x; std::cout << readNumber(x) << "\n"; }}
Cmain.c · 55 行
#include <stdio.h>#include <string.h>// 한글 한 글자는 UTF-8로 3바이트다. 문자열은 그냥 바이트로 이어 붙인다static const char *DIGIT[] = {"", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"};static const char *PLACE[] = {"천", "백", "십"};static const int PLACE_SIZE[] = {1000, 100, 10};static const char *GROUP[] = {"조", "억", "만", ""};static const long long GROUP_SIZE[] = {1000000000000LL, 100000000LL, 10000LL, 1LL};// 1 ~ 9999를 읽어 out 끝에 붙인다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천)void read_group(int n, char *out) { for (int i = 0; i < 3; i++) { int d = n / PLACE_SIZE[i] % 10; if (d == 1) { strcat(out, PLACE[i]); } else if (d > 1) { strcat(out, DIGIT[d]); strcat(out, PLACE[i]); } } strcat(out, DIGIT[n % 10]);}// 999조는 int(약 21억)를 넘으니 long long으로 받는다void read_number(long long x, char *out) { out[0] = '\0'; if (x == 0) { strcpy(out, "영"); return; } for (int i = 0; i < 4; i++) { int g = (int)(x / GROUP_SIZE[i] % 10000); if (g == 0) continue; // 빈 묶음은 단위까지 통째로 건너뛴다 if (g == 1 && i == 2) { strcat(out, "만"); // 10000은 "일만"이 아니라 "만" } else { read_group(g, out); strcat(out, GROUP[i]); } }}int main(void) { int n; long long x; char out[256]; // 가장 긴 것도 한글 40글자 남짓(120바이트) scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &x); read_number(x, out); puts(out); } return 0;}
Craykonmain.cray · 57 行
숫자이름 = ["", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"];// 1 ~ 9999를 읽는다. 십·백·천 앞의 1은 읽지 않는다(십, 백, 천)네자리읽기(n){ out = ""; 자리이름 = ["천", "백", "십"]; 자리크기 = [1000, 100, 10]; 循环 (i = 0; i < 3; i++) { d = 数学.商(n, 자리크기[i]) % 10; 如果 (d == 1) { out = out + 자리이름[i]; } 否则 如果 (d > 1) { out = out + 숫자이름[d] + 자리이름[i]; } } 返回 out + 숫자이름[n % 10];}// 999조까지는 2^53보다 작아서 수가 정확하다수읽기(x){ 如果 (x == 0) { 返回 "영"; } out = ""; 묶음이름 = ["조", "억", "만", ""]; 묶음크기 = [1000000000000, 100000000, 10000, 1]; 循环 (i = 0; i < 4; i++) { g = 数学.商(x, 묶음크기[i]) % 10000; 如果 (g == 0) { 继续; // 빈 묶음은 단위까지 통째로 건너뛴다 } 如果 (g == 1 && 묶음이름[i] == "만") { out = out + "만"; // 10000은 "일만"이 아니라 "만" } 否则 { out = out + 네자리읽기(g) + 묶음이름[i]; } } 返回 out;}n = 控制台.读行().转数字();循环 (k = 0; k < n; k++){ 控制台.输出(수읽기(控制台.读行().转数字()));}