forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintToRoman.cpp
More file actions
53 lines (46 loc) · 1.2 KB
/
intToRoman.cpp
File metadata and controls
53 lines (46 loc) · 1.2 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
53
#include <iostream>
#include <string>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
class Solution {
public:
string intToRoman(int num) {
string s = "IVXLCDM#";
int i1 = 0, i5 = 1;
string ret = "";
while (num > 0) {
int t = (num % 10);
num /= 10;
string ts = "";
if (t == 0) {
} else if (t >= 1 && t <= 3) {
REP(i,t) {
ts += s[i1];
}
} else if (t == 4) {
ts += s[i1];
ts += s[i5];
} else if (t <= 8) {
ts += s[i5];
REP(i,t - 5) ts += s[i1];
} else {
ts += s[i1];
ts += s[i1 + 2];
}
ret = ts + ret;
i1 += 2;
i5 += 2;
}
return ret;
}
};
int main() {
Solution s = Solution();
FOR(i,1,20) cout << s.intToRoman(i) << endl;
cout << s.intToRoman(3999) << endl;
cout << s.intToRoman(3645) << endl;
cout << s.intToRoman(2300) << endl;
cout << s.intToRoman(2444) << endl;
return 0;
}