Last active
December 26, 2015 14:59
-
-
Save avevlad/7169261 to your computer and use it in GitHub Desktop.
Римские числа http://www.e-olimp.com/problems/4103
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.e-olimp.com/problems/4103