Roman to Integer

来源:互联网 发布:人工智能包含哪些产业 编辑:程序博客网 时间:2024/05/16 05:53


Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.


int romanToInt(string s){    map<char, int> simbol;    simbol['I'] = 1;    simbol['V'] = 5;    simbol['X'] = 10;    simbol['L'] = 50;    simbol['C'] = 100;    simbol['D'] = 500;    simbol['M'] = 1000;    int re = 0;    for(int i=0; i<s.size(); i++)    {        if(i != s.size()-1)        {            if(simbol[s[i]] < simbol[s[i+1]])                re -= simbol[s[i]];            else                re += simbol[s[i]];        }        else            re += simbol[s[i]];    }    return re;}


0 0
原创粉丝点击