-
Notifications
You must be signed in to change notification settings - Fork 1
/
1996 S4 - When In Rome.cpp
183 lines (139 loc) · 4.21 KB
/
1996 S4 - When In Rome.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
1996 S4 - When In Rome
Difficulty: Easy/Medium
Just do it lmao.
*/
#include <iostream>
#include <string>
#include <map>
int main(){
std::map<char, int> romanToInt;
romanToInt['I'] = 1;
romanToInt['V'] = 5;
romanToInt['X'] = 10;
romanToInt['L'] = 50;
romanToInt['C'] = 100;
romanToInt['D'] = 500;
romanToInt['M'] = 1000;
int cases;
std::cin >> cases;
while (cases){
std::string input;
std::cin >> input;
std::string left = "", right = "";
char current = 'l';
//Iterate through input, split into two substrings, left side of operator and right side
for (int i = 0; i < input.length() - 1; i++){
if (input[i] == '+'){
current = 'r';
continue;
}
if (current == 'l'){
left += input[i];
}
else{
right += input[i];
}
}
//Convert the 2 roman numerals into actual numbers
//Left side of operator
int LEFT = 0;
LEFT += romanToInt[left[0]];
char previous = left[0];
for (int i = 1; i < left.length(); i++){
if (romanToInt[left[i]] > romanToInt[previous]){
LEFT -= romanToInt[previous];
LEFT += romanToInt[left[i]] - romanToInt[previous];
}
else{
LEFT += romanToInt[left[i]];
}
previous = left[i];
}
//Right side of operator
int RIGHT = 0;
RIGHT += romanToInt[right[0]];
previous = right[0];
for (int i = 1; i < right.length(); i++){
if (romanToInt[right[i]] > romanToInt[previous]){
RIGHT -= romanToInt[previous];
RIGHT += romanToInt[right[i]] - romanToInt[previous];
}
else{
RIGHT += romanToInt[right[i]];
}
previous = right[i];
}
int sum = LEFT + RIGHT;
//Convert sum to roman numeral
if (sum > 1000){
std::cout << input << "CONCORDIA CUM VERITATE\n";
}
//Since roman numerals are constructed using the characters from greatest to least
//It suffices to just greedily construct the roman numberal by using the largest character possible to fill the remaining sum required
else{
std::string output = "";
while (true){
if (sum == 0){
break;
}
if (sum >= 1000){
output += 'M';
sum -= 1000;
}
else if (sum >= 900){
output += "CM";
sum -= 900;
}
else if (sum >= 500){
output += 'D';
sum -= 500;
}
else if (sum >= 400){
output += "CD";
sum -= 400;
}
else if (sum >= 100){
output += 'C';
sum -= 100;
}
else if (sum >= 90){
output += "XC";
sum -= 90;
}
else if (sum >= 50){
output += 'L';
sum -= 50;
}
else if (sum >= 40){
output += "XL";
sum -= 40;
}
else if (sum >= 10){
output += 'X';
sum -= 10;
}
else if (sum >= 9){
output += "IX";
sum -= 9;
}
else if (sum >= 5){
output += 'V';
sum -= 5;
}
else if (sum >= 4){
output += "IV";
sum -= 4;
}
else{
output += 'I';
sum -= 1;
}
}
std::cout << input << output << '\n';
}
//std::cout << LEFT << ' ' << RIGHT << '\n';
cases--;
}
return 0;
}