-
Notifications
You must be signed in to change notification settings - Fork 213
/
IntegertoRoman.h
44 lines (42 loc) · 1.38 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
/*
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_1(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;
}
string intToRoman_2(int num) {
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string res;
for (int i = 0; i < 13; ++i) {
while (num >= values[i]) {
num -= values[i];
res += numerals[i];
}
}
return res;
}
};