Skip to content

Commit fe1c9ac

Browse files
committed
.\intToRoman.cpp
1 parent d0eee26 commit fe1c9ac

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

LeetCode/intToRoman.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
#define REP(i,n) for(int i=0;i<(n);++i)
6+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
7+
8+
class Solution {
9+
public:
10+
string intToRoman(int num) {
11+
string s = "IVXLCDM#";
12+
int i1 = 0, i5 = 1;
13+
string ret = "";
14+
while (num > 0) {
15+
int t = (num % 10);
16+
num /= 10;
17+
18+
string ts = "";
19+
if (t == 0) {
20+
} else if (t >= 1 && t <= 3) {
21+
REP(i,t) {
22+
ts += s[i1];
23+
}
24+
} else if (t == 4) {
25+
ts += s[i1];
26+
ts += s[i5];
27+
} else if (t <= 8) {
28+
ts += s[i5];
29+
REP(i,t - 5) ts += s[i1];
30+
} else {
31+
ts += s[i1];
32+
ts += s[i1 + 2];
33+
}
34+
35+
ret = ts + ret;
36+
37+
i1 += 2;
38+
i5 += 2;
39+
}
40+
41+
return ret;
42+
}
43+
};
44+
45+
int main() {
46+
Solution s = Solution();
47+
FOR(i,1,20) cout << s.intToRoman(i) << endl;
48+
cout << s.intToRoman(3999) << endl;
49+
cout << s.intToRoman(3645) << endl;
50+
cout << s.intToRoman(2300) << endl;
51+
cout << s.intToRoman(2444) << endl;
52+
return 0;
53+
}

0 commit comments

Comments
 (0)