Convert Number Into Roman Numeral
来源:互联网 发布:域名含义查询 编辑:程序博客网 时间:2024/05/22 00:38
Convert the given number into a roman numeral
JavaScript impl (with comment):
/* param: num just give a number between 1 ~ 3999*/function convertToRoman(num) { // parse int number to a string var numStr = num+""; // construct roman numeral map var roman = { 1:"I", 5:"V", 10:"X", 50:"L", 100:"C", 500:"D", 1000:"M" }; /* iterate the given string from the end to start, loop once that variable bit will times 10, this will select the corresponse value in object roman */ for(var i=numStr.length-1,bit = 1,res =[];i>=0;i--,bit*=10){ var ch = numStr.charAt(i); var element = convertToRomanNumWithBit(roman,ch,bit); res.push(element); } /* since iterate the given number from the end to start just now, so reverse the array makes the order of number correct , and return a new string that store the roman numeral */ num = res.reverse().join(''); return num;}/* param : romanNumTable , ch -- the 1-digit number or string , bit -- the bit of ch , demo: 2567 ch = 5, bit = 100*/function convertToRomanNumWithBit(romanNumTable,ch,bit){ var str=""; if(ch<4) str = romanNumTable[1*bit].repeat(parseInt(ch)); else if(ch == 5) str = romanNumTable[5*bit]; else if(ch == 4) str = romanNumTable[1*bit].concat(romanNumTable[5*bit]); else if(ch == 9) str = romanNumTable[1*bit].concat(romanNumTable[1*bit*10]); else str = romanNumTable[5*bit].concat(romanNumTable[1*bit].repeat(parseInt(ch)-5)); return str;}
0 0
- Convert Number Into Roman Numeral
- Given a roman numeral, convert it to an integer.
- Roman Numeral Converter
- Roman Numeral Converter
- Roman Numeral Converter
- Roman Numeral Converter
- Bonfire: Roman Numeral Converter 阿拉伯 转罗马
- FCC--Roman Numeral Converter(罗马数字的转换)
- Roman Numeral Converter--将给定的数字转换为罗马数字
- Convert number
- convert div into image
- Roman number -- 罗马数字
- convert div content into image
- Convert dictionary values into array
- convert an int into a byte array
- How to convert smartform output into pdf?
- Convert anonymous type into any type
- can't convert ActiveRecord::RecordInvalid into String
- StringIndexOutOfBoundsException
- 简单完美解决PullToRefreshLayout中PullableListView嵌套ViewPager时滑动冲突的问题
- MyEclipse 6.5 Blue Edition - Milestone 1版本获取注册码(2)
- 计算机原理笔记1
- SQL基础--基础语句和运算
- Convert Number Into Roman Numeral
- 线程与进程的区别
- 第十四周项目2--二叉树排序树中查找的路径
- 合并排序-非递归
- 使用c语言调用python小结
- 高精度乘法(小数)
- SQL基础--基础函数
- iOS知识点随笔
- day1学习中遇到的问题