forked from leetcoders/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegertoRoman.h
32 lines (30 loc) · 977 Bytes
/
IntegertoRoman.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Author: Annie Kim, [email protected]
Date: May 13, 2013
Problem: Integer to Roman
Difficulty: Easy
Source: http://leetcode.com/onlinejudge#question_12
Notes:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Solution: Buffer the roman numbers.
*/
class Solution {
public:
const string rom[4][10] = {{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}};
string intToRoman(int num) {
string res;
int i = 3;
while (num > 0)
{
int divisor = (int)pow(10, i);
res += rom[i][num / divisor];
num %= divisor;
i--;
}
return res;
}
};