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

罗马数字的规则:

罗马数字网址

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. 计算属性和监听,computed,watch

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Python学习之旅(十八)

    Python基础知识(17):面向对象编程(Ⅱ) 获取对象信息 在不知道对象信息的情况下,我们想要去获取对象信息,可以使用以下方法 1.type (1)判断对象类型 >>> type ...

  3. css的position,float属性的理解

    我们知道,html是按照普通流来加载的,这个时候我们有些需求就不好实现.因此出现了非普通流: 1.普通流:按照顺序正常的排列,长度或不够就往下挤.position默认的static 2.非普通流:脱离 ...

  4. sale.order

    # 初始化一个变量用来记录产品类型line_type = ''# 循环明细行for product in self.options: # 拿到该明细行的产品类型 product_type = prod ...

  5. Java连接MySQL报出警告 WARN: Establishing SSL connection without server's identity verification is not recommended.

    很多人使用JDBC连接MySQL时报出警告: WARN: Establishing SSL connection without server's identity verification is n ...

  6. Spring中IOC和AOP的详细解释(转)

    原文链接:Spring中IOC和AOP的详细解释 我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂 ...

  7. java框架之Spring(3)-JDBC模板使用&事务管理

    下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...

  8. python基础之 异常处理和logging模块

    1.异常处理 l = ['apple','admin','kobe'] for id,item in enumerate(l,1): print(id,item) try: choose_id = i ...

  9. repo 获取各个库的tag代码或者分支代码

    关于mainfest.xml中的参数格式和说明,可以自己查阅,此处不详细写,我们知道project中的reversion可以指定分支,tag,commitid等,那么如何书写呢? 首先克隆mainfe ...

  10. oracle中实现某个用户truncate 其它用户下的表

    oracle文档中对truncate权限的要求是需要某表在当前登录的用户下,或者当前登录的用户有drop any table的权限. 但是如果不满足第一个条件的情况下,要让某用户满足第二个条件就导致权 ...