Skip to content

Instantly share code, notes, and snippets.

@avevlad
Last active Dec 26, 2015
Embed
What would you like to do?
Римские числа http://www.e-olimp.com/problems/4103
#include <iostream>
#include <string>
unsigned getDecimal(char val)
{
switch (val) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: throw "Undefined character!";
}
return val;
}
unsigned getInt(std::string s)
{
unsigned n = 0, result = 0, last = 0, tmp = 0;
size_t len = s.size();
for (int i = len - 1; 0 <= i; --i) {
tmp = getDecimal(s[i]);
if (tmp < last) {
result -= tmp;
} else {
result += tmp;
}
last = tmp;
}
return result;
}
int main()
{
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
std::string s;
std::cin>> n;
for (int i=0; i < n; ++i) {
std::cin>> s;
std::cout<< getInt(s) <<std::endl;
}
}
@avevlad

This comment has been minimized.

Copy link
Owner Author

@avevlad avevlad commented Oct 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment