fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int romanToInt(string s) {
  5. map<char, int> m;
  6. m['I'] = 1;
  7. m['V'] = 5;
  8. m['X'] = 10;
  9. m['L'] = 50;
  10. m['C'] = 100;
  11. m['D'] = 500;
  12. m['M'] = 1000;
  13. int len = s.length();
  14. if(len <= 0)
  15. return 0;
  16. int result = m[s[len - 1]];
  17. for(int i = len - 2; i >= 0; i--){
  18. if(m[s[i]] < m[s[i+1]]){
  19. result = result - m[s[i]];
  20. }else{
  21. result = result + m[s[i]];
  22. }
  23. }
  24. return result;
  25. }
  26.  
  27. int main() {
  28. cout << romanToInt("XIV");
  29. return 0;
  30. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
14