问题描述:(将阿拉伯数字转换成罗马数字)

Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.

Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.

Example:

solution(1000); // should return 'M'

Help:

Symbol    Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
Remember that there can't be more than 3 identical symbols in a row.

优秀答案:

 function solution(number){
// convert the number to a roman numeral
var roman = {M:1000,CM:900, D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1 } var ans = '';
while(number>0){
for(a in roman){
if(roman[a]<=number){ ans += a; number-=roman[a]; break;} }
}
return ans;
}

codewars--js--Roman Numerals Encode的更多相关文章

  1. Project Euler 89:Roman numerals 罗马数字

    Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...

  2. Roman numerals

    Roman numerals 罗马数字的题目, 注意几个关键的数字即可: (100, 400, 500, 900) -> ('C', 'CD', 'D', 'CM'); (10, 40, 50, ...

  3. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  4. CodeForcesGym 100641D Generalized Roman Numerals

    Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on  ...

  5. Roman Numerals All In One

    Roman Numerals All In One 罗马数字 refs https://www.mathsisfun.com/roman-numerals.html https://www.maths ...

  6. [CodeWars][JS]实现链式加法

    在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...

  7. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

  8. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  9. UVA - 185 Roman Numerals

    题目链接: https://vjudge.net/problem/UVA-185 思路: 剪枝.回溯 注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题! 思路参考 ...

随机推荐

  1. 把 CPU “玩”起来

    前言 从开始学习编程之后,就渐渐痴迷于技术,平时遇到购书满减活动时就忍不住买一堆书.前两天闲着无聊,翻开了去年买的<编程之美>,目录里的“让 CPU 占用率听你指挥”吸引力我的眼球.这一年 ...

  2. windows下远程访问Linux系统中mysql

    1,查询MySQL数据库是否允许远程ip访问,命令如下: sql语句: use mysql; select host, user from user; 查询结果为127.0.0.1或者localhos ...

  3. 学习 lind 语 里的一些组件使用。

    step one autofac : link:http://files.cnblogs.com/files/ganmk--jy/AutofacTest.rar step two 还有很多

  4. 个人第四次作业AIpha2版本测试(最终版)

    这个作业属于哪个课程 软件工程 作业要求在哪里 作业要求 团队名称 RainbowPlan团队博客 这个作业目标 手动测试非本团队的小组程序,是否可以正常登录,正常运行 一.测试人员信息 测试人员 姓 ...

  5. 剑指Offer对答如流系列 - 重建二叉树

    面试题6:重建二叉树 题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8} ...

  6. Windows版Redis主从配置

    一.下载 从github上下载Redis的zip包,地址:https://github.com/MicrosoftArchive/redis/releases Redis本身不支持windows,这是 ...

  7. C语言秋季作业4

    本周作业头 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 作业连接 我在这个课程的目标是 熟练掌握for循环语句的运用 这个作业在那个具体方面帮助我实现目标 通过作业练习 参考文献 文 ...

  8. C语言系列之实验楼笔记(一)

    创建C程序的几个过程: 1.编辑:创建和修改C程序的源代码 2.编译:编译器可以将源代码转成机器语言.linux 这些文件扩展名.o 3.链接:通过一次完成编译和链接 4.执行;运行程序 打开xfce ...

  9. 实现当前目录下开启CMD

    我们都知道在WIN7下,可以按shift+鼠标右键->在此处打开命令窗口 或者在 输入cmd,回车 那么,怎样去实现这样的功能呢? //当前目录下开启CMD #include <stdio ...

  10. LeetCode 547. Friend Circles 朋友圈(C++/Java)

    题目: https://leetcode.com/problems/friend-circles/ There are N students in a class. Some of them are ...