|
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string roman(int n)
{
string str;
char n1, n5, n10;
if(n < 10)
{
n1 = 'I';
n5 = 'V';
n10 = 'X';
}
else if(n < 100)
{
n1 = 'X';
n5 = 'L';
n10 = 'C';
n /= 10;
}
else if(n < 1000)
{
n1 = 'C';
n5 = 'D';
n10 = 'M';
n /= 100;
}
else
{
n1 = 'M';
n /= 1000;
}
switch(n)
{
case 1:
return str.append(1,n1);
break;
case 2:
return str.append(2,n1);
break;
case 3:
return str.append(3,n1);
break;
case 4:
str += n1;
str += n5;
return str;
break;
case 5:
return str.append(1,n5);
break;
case 6:
str += n5;
str += n1;
return str;
break;
case 7:
str += n5;
str.append(2,n1);
return str;
break;
case 8:
str += n5;
str.append(3,n1);
return str;
break;
case 9:
str += n1;
str += n10;
return str;
break;
}
}
string toRoman(int n)
{
string result;
int digit = 0;
while(n)
{
while(!(n%10))
{
digit++;
n /= 10;
}
if(digit)
{
result.insert(0,roman((n%10)*pow(10,digit++)));
}
else
{
result.insert(0,roman(n%10));
digit++;
}
n /= 10;
}
return result;
}
int main()
{
int n;
while(cin >> n)
{
cout << n << " = " << toRoman(n) << endl;
}
return 0;
}
|