-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToRoman.cpp
More file actions
52 lines (49 loc) · 1.24 KB
/
IntegerToRoman.cpp
File metadata and controls
52 lines (49 loc) · 1.24 KB
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
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <stack>
#include <string>
#include <assert.h>
#include <list>
#include <map>
#include <vector>
using namespace std;
class Solution {
public:
string toRoman(int num, int level){
static char digits[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
string str;
if(num <= 3){
for(int i=0; i<num; i++){
str.push_back(digits[level*2]);
}
} else if(num >=5 && num <9){
str.push_back(digits[level*2+1]);
for(int i=0; i<num-5; i++)
str.push_back(digits[level*2]);
} else if(num == 4){
str.push_back(digits[level*2]);
str.push_back(digits[level*2+1]);
} else if(num == 9){
str.push_back(digits[level*2]);
str.push_back(digits[level*2+2]);
}
return str;
}
string intToRoman(int num){
int level = 0;
string ret;
while(num != 0){
int remain = num %10;
ret = toRoman(remain, level) + ret;
num /= 10;
level++;
}
return ret;
}
};
int main(int argc, char *argv[])
{
Solution sol;
std::cout << sol.intToRoman(1230) << std::endl;
return 0;
}