代码高尔夫:从数字到单词 [英] Code Golf: Number to Words
本文介绍了代码高尔夫:从数字到单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码高尔夫系列似乎还算流行.我遇到了一些将数字转换为其单词表示的代码.一些例子是(编程乐趣的 2 的幂):
The code golf series seem to be fairly popular. I ran across some code that converts a number to its word representation. Some examples would be (powers of 2 for programming fun):
- 2 -> 两个
- 1024 -> 一千二十四
- 1048576 -> 一百万四千八千五七十六
我的同事提出的算法几乎有两百行长.似乎会有更简洁的方法来做到这一点.
The algorithm my co-worker came up was almost two hundred lines long. Seems like there would be a more concise way to do it.
当前指南:
- 欢迎以任何编程语言提交(我向PhiLho 最初对此缺乏明确性)
- 最大输入为 2^64(请参阅以下链接了解单词,感谢 mmeyers)
- 小规模,首选英文输出,但欢迎使用任何算法.只需对所使用的方法与编程语言一起进行评论即可.
- Submissions in any programming language welcome (I apologize to PhiLho for the initial lack of clarity on this one)
- Max input of 2^64 (see following link for words, thanks mmeyers)
- Short scale with English output preferred, but any algorithm is welcome. Just comment along with the programming language as to the method used.
推荐答案
Lisp,只使用标准函数:
Lisp, using only standard functions:
(format nil "~r" 1234) ==> "one thousand two hundred thirty-four"
奖励:
(format nil "~@r" 1234) ==> "MCCXXXIV"
这篇关于代码高尔夫:从数字到单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文