Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int convert(char c) {
- if ((c == 'I') || (c == 'V') || (c == 'X') || (c == 'L') || (c == 'C') || (c == 'D') || (c == 'M')) {
- switch (c) {
- case 'M': return 1000;
- case 'D': return 500;
- case 'C': return 100;
- case 'L': return 50;
- case 'X': return 10;
- case 'V': return 5;
- case 'I': return 1;
- default: return 0;
- }
- }
- else return 0;
- }
- int translate(const string str) {
- int res = 0; int tmp = 0;
- bool status = true;
- for (int i = 0; i < size(str); i++) {
- int x = convert(str[i]);
- if (tmp == 0) {
- tmp = x;
- res += x;
- }
- else
- {
- if (tmp >= x) {
- res += x;
- }
- else {
- res += x - 2*tmp;
- }
- tmp = x;
- }
- }
- return res;
- }
- int main() {
- string str;
- cin >> str;
- cout << translate(str);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement