leetcode刷题,总结, 记录,备忘 12
来源:互联网 发布:罗杰疑案知乎 编辑:程序博客网 时间:2024/06/03 09:16
leetcode12Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999
之前那个题的姊妹篇,首先强调!没什么难的,,,把罗马数字的规则搞搞清楚就行了,,,我写的代码写的太多了,,,全是if,else if各种判断条件,,感觉写的很不好,虽然通过了,,有空去看看别人是怎么写的。。。。具体详细就不描述了,,还是那一老套的东西,,关键搞清楚罗马数字的规则就行了,还是很简单的。
class Solution {public: string intToRoman(int num) { int temp, m, d = 1000, count = 4; string s; while (count) { temp = num / d; if (!temp) { count--; d /= 10; continue; } num %= d; if (d == 1000) s += string(temp, 'M'); else if (d == 100) { if (temp == 9) s += "CM"; else if (temp > 5 && temp < 9) s += "D" + string(temp-5, 'C'); else if (temp < 5 && temp > 3) s += string(5-temp, 'C') + "D"; else if (temp == 5) s += "D"; else s += string(temp, 'C'); } else if (d == 10) { if (temp == 9) s += string("XC"); else if(temp < 9 && temp > 5) s += "L" + string(temp-5, 'X'); else if (temp < 5 && temp > 3) s += string(5-temp, 'X') + 'L'; else if (temp == 5) s += "L"; else s += string(temp, 'X'); } else if (d == 1) { if (temp == 9) s += "IX"; else if (temp > 5 && temp < 9) s += "V" + string(temp-5, 'I'); else if(temp == 5) s += "V"; else if (temp == 4) s += "IV"; else if (temp < 4) s += string(temp-0, 'I'); } count--; d /= 10; } return s; }};
0 0
- leetcode刷题,总结, 记录,备忘 12
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- 一个被 -webkit-box 坑了的娃
- 关于SAP中的记账码的解释
- 欢迎使用CSDN-markdown编辑器
- uc/os-ii任务调度(一)
- Linux中基本I/O 重定向的符号及其用法和文件标识符
- leetcode刷题,总结, 记录,备忘 12
- 复制代码片段
- [VC++]_[初级]_[使用zlib标准库解压zip文件]
- ORA-01274: cannot add datafile '/oradata/orauat/tbs_captain01.dbf'
- OOP
- Hadoop中的进程与Mapper实例,Reducer实例
- VS中语言属性对字符集转码的影响
- awk1line
- 通过浏览器直接打开iOS/Android App 应用程序 - Cundong's log - 开源中国社区