做数字转罗马数字时,用到了贪心算法,很好用,记录一下,有时间系统的学一下

罗马数字的规则:

罗马数字网址

1 5 10 50 100 500 1000
I V X L C D M1

1当一个符号在一个 比它大的符号后面(右边),就要 加上这符号的值

  • 例子: VI = V + I = 5 + 1 = 6

2当一个符号在一个 比它大的符号前面(左边),就要 减去这符号的值

  • 例子: IX = X - I = 10 - 1 = 9

3不要连续使用同一符号超过三次 (但 IIII 有时是用来代表 4,尤其在时钟上)

我刚开始考虑用switch 然后再对加减情况单独处理,发现太多了,根本考虑不全;

后来参考了罗马数字和整数相互转换这篇文章,发现反过来想,这个问题就很简单

    1000 900 500 400 100 90 50 40 10 9 5 4 1
M CM D CD C XC L XL X IX V IV I

然后将数字与这个数组对应,选最近的值加上他的罗马数字,简单粗暴的就过了,

function convert(num) {

  var str="";
var bignum=[1000,900,500,400,100,90,50,40,10,9,5,4,1];
var roman=["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];
var i=0;
while(num>0){
if(num>=bignum[i]){
num-=bignum[i];
str+=roman[i];
}else {
i++;
}
}
return str;
} convert(97);

freecodecamp数字转化成罗马数字的更多相关文章

  1. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  2. LeetCode第十二题-将数字转化为罗马数字

    Integer to Roman 问题简介:将输入的int类型数字转化为罗马数字 问题详解:罗马数字由七个不同的符号表示:I,V,X,L,C,D和M 符号-数值 I - 1 V - 5 X -10 L ...

  3. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  4. Python代码阅读(第2篇):数字转化成列表

    本篇阅读的代码实现了将输入的数字转化成一个列表,输入数字中的每一位按照从左到右的顺序成为列表中的一项. 本篇阅读的代码片段来自于30-seconds-of-python. digitize def d ...

  5. 整理用Java实现数字转化成字符串左边自动补零方法

    Java 中给数字左边补0 (1)方法一 import java.text.NumberFormat; public class NumberFormatTest { public static vo ...

  6. [LintCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...

  7. 数字转化成字符串C语言

    #include <stdio.h> void Myitoa(int,char *); int getnumberLength(int); int main(){ ]; ; Myitoa( ...

  8. sql 取值时将字符串类型数字转化成整形

    select cast(a.Vchcode as int) as avchcode,a.ptypeid,a.assqty,unit,b.pfullname,b.standard,b.type from ...

  9. leetcode:Integer to Roman(整数转化为罗马数字)

    Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...

随机推荐

  1. history.back();谷歌浏览器,iframe后退问题

    history.back();谷歌浏览器,iframe后退直接会后退父页面. 使用以下方式即可//document.referrer是获取上一页的urllocation.href=document.r ...

  2. loadrunner笔记(二):飞机订票系统--客户信息注册

    (一)  几个重要概念说明 集合点:同步虚拟用户,以便同一时间执行任务. 事务:事务是指服务器响应用户请求所用的时间,当然它可以衡量某个操作,如登录所需要的时间,也可以衡量一系列的操作所用的时间,如从 ...

  3. 嵌入式常用技术概览之SPI

    一.先决知识             (1)理解并知道移位寄存器如何工作 二.SPI概述             SPI(serial Peripheral Interface 串行片上(外围)设备接 ...

  4. xargs实例

    1. 当你使用rm命令去删除很多的文件时,你可能会得到错误信息:“/bin/rm Argument list too long – Linux”.这时可以用xargs来避免这个问题 find ~ -n ...

  5. HTML基础之HTML标签-html header(meta,title) html body(p,br,h,form,div,span,input,lable)

    摘自:http://www.imdsx.cn/index.php/2017/07/27/html0/ 一.HTML标签 <!DOCTYPE html> <!--标准的html规则,类 ...

  6. SQL两张表筛选相同数据和不同数据

    原文链接:http://www.cnblogs.com/onesmail/p/6148979.html 方法一: select distinct A.ID from A where A.ID not ...

  7. SQL[Err]ORA-00932: inconsistent datatypes: expected NUMBER got CHAR:

    ORA-00932: inconsistent datatypes: expected NUMBER got CHAR: 获取的目标类型与源类型不一致,多出现在case when 语句中,when的结 ...

  8. Spark application注册master机制

    直接上Master类的代码: case RegisterApplication(description) => { if (state == RecoveryState.STANDBY) { / ...

  9. 亿图图示 Edraw Max v9.2 完美破解版

    主程序:http://www.edrawsoft.cn/2download/edrawmax-cn-9.2.exe破解补丁:https://www.lanzous.com/i1fjsyh 密码:52p ...

  10. 20190422 DW/BI系统

    其实很多人都不能理解操作型应用系统和分析型系统的意义,主要问题在于这两个系统所面对的用户和需求是不相同的. 建模重点“凡事应该简单,简单到不能再简单”