[LeetCode] Integer to Roman 解题报告
来源:互联网 发布:ug10.0加工编程铣平面 编辑:程序博客网 时间:2024/05/14 14:32
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
» Solve this problem
[解题思路]
这道题主要就在于如何处理每一位digit,并且按照区间不同处理:
1<=digit <=3
digit =4
digit = 5
5<digit<=8
digit =9
[Code]
1: string intToRoman(int num) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: char symbol[7] = { 'I','V','X', 'L','C', 'D','M'};
5: string roman;
6: int scale = 1000;
7: for(int i =6; i>=0; i-=2)
8: {
9: int digit = num/scale;
10: if(digit != 0)
11: {
12: if(digit <= 3)
13: {
14: roman.append(digit, symbol[i]);
15: }
16: else if(digit ==4)
17: {
18: roman.append(1, symbol[i]);
19: roman.append(1, symbol[i+1]);
20: }
21: else if(digit ==5)
22: {
23: roman.append(1, symbol[i+1]);
24: }
25: else if(digit <=8)
26: {
27: roman.append(1, symbol[i+1]);
28: roman.append(digit-5, symbol[i]);
29: }
30: else if(digit ==9)
31: {
32: roman.append(1, symbol[i]);
33: roman.append(1, symbol[i+2]);
34: }
35: }
36: num = num%scale;
37: scale/=10;
38: }
39: return roman;
40: }
0 0
- 【LeetCode】Integer to Roman 和 Roman to Integer 解题报告
- 【LeetCode】Integer to Roman 和 Roman to Integer 解题报告
- [LeetCode]Roman to Integer,解题报告
- [LeetCode]Integer to Roman,解题报告
- LeetCode解题报告--Integer to Roman
- LeetCode解题报告--Roman to Integer
- 【LeetCode】Roman to Integer 解题报告
- 【LeetCode】Integer to Roman 解题报告
- [leetcode] 13. Roman to Integer 解题报告
- [LeetCode] Integer to Roman 解题报告
- [LeetCode] Roman To Integer 解题报告
- [leetcode] 12. Integer to Roman 解题报告
- LeetCode 13. Roman to Integer 解题报告
- LeetCode 12. Integer to Roman 解题报告
- [Leetcode] 12. Integer to Roman 解题报告
- [Leetcode] 13. Roman to Integer 解题报告
- [LeetCode] Integer to Roman 解题报告
- [LeetCode] Roman to Integer 解题报告
- pip常用操作
- [LeetCode] Generate Parentheses 解题报告
- [LeetCode] Gray Code 解题报告
- [LeetCode] Implement strStr() 解题报告
- [LeetCode] Insert Interval 解题报告
- [LeetCode] Integer to Roman 解题报告
- [LeetCode] Interleaving String 解题报告
- 采用dlopen、dlsym、dlclose加载动态链接库【总结】
- [LeetCode] Jump Game 解题报告
- [LeetCode] Jump Game II 解题报告
- [LeetCode] Largest Rectangle in Histogram 解题报告
- [Leetcode] Length of Last Word 解题报告
- [LeetCode] Letter Combinations of a Phone Number 解题报告
- [LeetCode] Longest Common Prefix 解题报告