Int to Roman
来源:互联网 发布:工程造价有什么软件 编辑:程序博客网 时间:2024/05/07 08:55
阿拉伯数字转罗马数字(1~3999)
思路:
直接从个位数开始转起,分别用10,100,1000,10000取余数,不同的余数采用不同的解决办法,最后返回字符串
代码
class Solution {public: string intToRoman(int num){ int d = 10; int n = num; string rslt(""); while (n > 0) { //给10取余数 int single = n % 10; switch (single) { case 0: break; case 4: rslt += "IV" ; break; case 5: rslt += "V" ; break; case 1: case 2:case 3: { int tmp = single; while (tmp > 0) { rslt += "I"; --tmp; } break; } case 6:case 7: case 8: { int tmp = single; rslt += "V"; while (tmp > 5) { rslt += "I"; --tmp; } break; } case 9: rslt += "IX"; break; } //减去 n = n - single; if (n == 0) return rslt; //给100取余数 int decade = n % 100; switch (decade) { case 40: rslt = "XL"+rslt; break; case 50: rslt = "L" + rslt; break; case 0: break; case 10: case 20:case 30: { int tmp = decade; while (tmp >= 10) { rslt = "X" + rslt; tmp -= 10; } break; } case 60:case 70: case 80: { int tmp = decade; string tmpRslt = "L"; while (tmp > 50) { tmpRslt += "X"; tmp -= 10; } rslt = tmpRslt + rslt; break; } case 90: { rslt = "XC" + rslt; break; } } //减去 n = n - decade; if (n == 0) return rslt; //给1000取余数 int hunderd = n % 1000; switch (hunderd) { case 0: break; case 400: rslt = "CD" + rslt; break; case 500: rslt = "D" +rslt; break; case 100: case 200:case 300: { int tmp = hunderd; while (tmp >= 100) { rslt = "C" + rslt; tmp -= 100; } break; } case 600:case 700: case 800: { int tmp = hunderd; string tmpRslt = "D"; while (tmp > 500) { tmpRslt += "C"; tmp-=100; } rslt = tmpRslt + rslt; break; } case 900: { rslt = "CM" + rslt; break; } } //减去 n = n - hunderd; if (n == 0) return rslt; //给10000取余数 int thousand = n % 10000; switch (thousand) { case 1000: rslt = "M" + rslt; break; case 2000: rslt = "MM" + rslt; break; case 3000:rslt = "MMM" + rslt; break; } n = n - thousand; } return rslt;}};
0 0
- Int to Roman And Roman to Int
- Int to Roman
- Roman to Int
- Roman to Int
- Int to Roman
- Int to Roman
- ROMAN TO INT
- Leetcode-12 int to Roman
- leetcode (12) - INT TO ROMAN
- leetcode (13) - ROMAN TO INT
- LeetCode算法题之int to Roman
- 13. Roman to Integer (罗马数字转int)
- Integer to Roman & Roman to Integer
- Integer to Roman & Roman to Integer
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- bzoj2251 外星联络
- COCOS 屏蔽一个layer
- cocos 进入后台的侦听器
- 【java】:java中获得本机ip地址和本机名
- React Native入门学习笔记一(环境搭建windows+android)
- Int to Roman
- solution Of 1051. Pop Sequence (25)
- U-Boot的配置、编译、连接过程
- sublime text 支持中文不乱码版下载
- cocos 动画整理
- Java TreeMap 源码解析
- 自定义View基础篇一
- 使用fiddler将制作的html网页放到api.bing.com域名下
- java提高篇(三十)-----Iterator