Blame view

P29253_en/S001-AC.cc 1011 Bytes
Imanol-Mikel Barba Sabariego authored
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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int toDecimal(string str)
{
	int n = 0;
	int i = 0;
	char n1, n5, n10;
	while(str[i] == 'M')
	{
		n += 1000;
		i++;
	}
	if(str[i] == 'C' && str[i+1] == 'M')
	{
		n += 900;
		i += 2;
	}
	if(str[i] == 'D')
	{
		n += 500;
		i++;
	}
	if(str[i] == 'C' && str[i+1] == 'D')
	{
		n += 400;
		i += 2;
	}
	while(str[i] == 'C')
	{
		n += 100;
		i++;
	}
	if(str[i] == 'X' && str[i+1] == 'C')
	{
		n += 90;
		i += 2;
	}
	if(str[i] == 'L')
	{
		n += 50;
		i++;
	}
	if(str[i] == 'X' && str[i+1] == 'L')
	{
		n += 40;
		i += 2;
	}
	while(str[i] == 'X')
	{
		n += 10;
		i++;
	}
	if(str[i] == 'I' && str[i+1] == 'X')
	{
		n += 9;
		i += 2;
	}
	if(str[i] == 'V')
	{
		n += 5;
		i++;
	}
	if(str[i] == 'I' && str[i+1] == 'V')
	{
		n += 4;
		i += 2;
	}
	while(str[i] == 'I')
	{
		n += 1;
		i++;
	}
	return n;
}

int main()
{
	string str;
	while(cin >> str)
	{
		str.erase(str.size()-1,1);
		cout << str << " = " << toDecimal(str) << endl;
	}
	return 0;
}