-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathInteger_to_roman.cpp
61 lines (54 loc) · 997 Bytes
/
Integer_to_roman.cpp
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
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;
string integer_to_Roman(int n) {
string str_romans[] = {
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I"
};
int values[] = {
1000,
900,
500,
400,
100,
90,
50,
40,
10,
9,
5,
4,
1
};
string result = "";
for (auto int i = 0; i < 13; ++i) {
while (n - values[i] >= 0) {
result += str_romans[i];
n -= values[i];
}
}
return result;
}
int main() {
int n = 7;
cout << "Integer " << n << " : Roman " << integer_to_Roman(7) << endl;
n = 19;
cout << "Integer " << n << " : Roman " << integer_to_Roman(19) << endl;
n = 789;
cout << "Integer " << n << " : Roman " << integer_to_Roman(789) << endl;
n = 1099;
cout << "Integer " << n << " : Roman " << integer_to_Roman(1099) << endl;
n = 23456;
cout << "Integer " << n << " : Roman " << integer_to_Roman(23456) << endl;
return 0;