Quick actions

cmd+k|ctrl+k

Navigation

Languages

romantodecimal

Snippet info

Language

Cpp

Visibility

public

Author

gsubhikaran

Created

2019-06-20T15:35:16Z

Updated

2019-06-20T15:35:16Z

#include <iostream>
using namespace std;
int value(char r) 
{ 
    if (r == 'I') 
        return 1; 
    if (r == 'V') 
        return 5; 
    if (r == 'X') 
        return 10; 
    if (r == 'L') 
        return 50; 
    if (r == 'C') 
        return 100; 
    if (r == 'D') 
        return 500; 
    if (r == 'M') 
        return 1000; 
  
    return -1; 
} 
int romanToDecimal(string &str) 
{ 
    // Initialize result 
    int res = 0; 
  
    // Traverse given input 
    for (int i=0; i<str.length(); i++) 
    { 
        // Getting value of symbol s[i] 
        int s1 = value(str[i]); 
  
        if (i+1 < str.length()) 
        { 
            // Getting value of symbol s[i+1] 
            int s2 = value(str[i+1]); 
  
            // Comparing both values 
            if (s1 >= s2) 
            { 
                // Value of current symbol is greater 
                // or equal to the next symbol 
                res = res + s1; 
            } 
            else
            { 
                res = res + s2 - s1; 
                i++; // Value of current symbol is 
                     // less than the next symbol 
            } 
        } 
        else
        { 
            res = res + s1; 
            i++; 
        } 
    } 
    return res; 
} 

int main() {
    string str;
    getline(cin,str);
    cout<<romanToDecimal(str);
    return 0;
}
INFO