英語でプリーズ!
締め切りが 2018/01/10 10:00 AM なので,その 1 分後に投稿されるように予約
【英語でプリーズ!】
与えられた整数値を、英語に変換するプログラムを作成してください。
たとえば"123"なら"One Hundred Twenty Three"、"-50000"のような負の数は、"Negative Fifty Thousand"のように出力してください。
必要な英単語は以下になります。
Zero
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
Thirteen
Fourteen
Fifteen
Sixteen
Seventeen
Eighteen
Nineteen
Twenty
Thirty
Forty
Fifty
Sixty
Seventy
Eighty
Ninety
Hundred
Thousand
Million
Billion
Negative
【入力】
標準入力から1行目には入力データ数N(1≦N≦100)が、2行目以降には整数値が与えられます。
2行目以降のN行分の整数値を英語に変換してください。
ただし、入力データは符号付き32bit整数の範囲で収まるものに限ります。
【出力】
標準出力に、変換後の英語を出力してください(入力データ毎に改行してください)。
アルファベットの大文字・小文字は問いません。
【入出力サンプル】
Input
7
123
4567
89012
0
-34
-5678901
1111111111
Output
One Hundred Twenty Three
Four Thousand Five Hundred Sixty Seven
Eighty Nine Thousand Twelve
Zero
Negative Thirty Four
Negative Five Million Six Hundred Seventy Eight Thousand Nine Hundred One
One Billion One Hundred Eleven Million One Hundred Eleven Thousand One Hundred Eleven
==============================
g = function(n) {
o1 = c("", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen")
o2 = c("Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
o3.1 = ""
o3.2 = c("One Hundred", "Two Hundred", "Three Hundred", "Four Hundred", "Five Hundred",
"Six Hundred", "Seven Hundred", "Eight Hundred", "Nine Hundred")
and = "" # \and""
a = c(t(outer(o3.1, c(o1, t(outer(o2, o1[1:10], paste))), paste)), t(outer(o3.2,
paste(and, c(o1, t(outer(o2, o1[1:10], paste)))), paste)), "One Thousand")[-1]
a[n]
}
f = function(n) {
if (n == 0) {
"Zero"
} else {
unit = c("Billion ", "Million ", "Thousand ", "")
m = integer(4)
negative = n < 0
n = abs(n)
for (i in 1:4) {
m[5 - i] = n%%1000
n = n%/%1000
}
a = ifelse(negative, "Negative", "")
for (i in 1:4) {
if (m[i] != 0) {
a = c(a, g(m[i]), unit[i])
}
}
a = paste(a, collapse = " ")
a = gsub(" +", " ", a)
a = sub("^ ", "", a)
sub(" $", "", a)
}
}
# arg = scan(file("stdin", "r"))[-1]
# for (i in arg) {
# cat(sprintf("%s\n", f(i)))
# }
f(123) # One Hundred Twenty Three
f(4567) # Four Thousand Five Hundred Sixty Seven
f(89012) # Eighty Nine Thousand Twelve
f(0) # Zero
f(-34) # Negative Thirty Four
f(-5678901) # Negative Five Million Six Hundred Seventy Eight Thousand Nine Hundred One
f(1111111111) # One Billion One Hundred Eleven Million One Hundred Eleven Thousand One Hundred Eleven
f(15674873) # Fifteen Million Six Hundred Seventy Four Thousand Eight Hundred Seventy Three
f(4620818) # Four Million Six Hundred Twenty Thousand Eight Hundred Eighteen
f(14440117) # Fourteen Million Four Hundred Forty Thousand One Hundred Seventeen
f(6868461) # Six Million Eight Hundred Sixty Eight Thousand Four Hundred Sixty One
f(14181126) # Fourteen Million One Hundred Eighty One Thousand One Hundred Twenty Six
f(-311541354) # Negative Three Hundred Eleven Million Five Hundred Forty One Thousand Three Hundred Fifty Four
f(504349000) # Five Hundred Four Million Three Hundred Forty Nine Thousand
f(126556530) # One Hundred Twenty Six Million Five Hundred Fifty Six Thousand Five Hundred Thirty
f(1301679771) # One Billion Three Hundred One Million Six Hundred Seventy Nine Thousand Seven Hundred Seventy One
f(-223594969) # Negative Two Hundred Twenty Three Million Five Hundred Ninety Four Thousand Nine Hundred Sixty Nine