-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.cpp
46 lines (41 loc) · 890 Bytes
/
7.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
#include <iostream>
#include <string>
using namespace std;
const string Rom[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I", ""};
const int Ar[]={1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1, 0};
string Roman(int arab)
{
string roman;
int i=0;
while(arab>0) {
while(Ar[i]<=arab) {
roman+=Rom[i];
arab-=Ar[i];
}
++i;
}
return roman;
}
int Arab(string roman)
{
int arab=0, i=0;
while(roman!="") {
while(Rom[i]==roman.substr(0, Rom[i].length())) {
roman.replace(0, Rom[i].length(), "");
arab+=Ar[i];
}
++i;
}
return arab;
}
int main()
{
string first, second;
char c;
while((c=getchar())!='+')
first.push_back(c);
while((c=getchar())!='\n')
second.push_back(c);
cout << Roman(Arab(first) + Arab(second))<<endl;
return 0;
}