标签:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
1 inline int c2n(char c) { 2 switch(c) { 3 case ‘I‘: return 1; 4 case ‘V‘: return 5; 5 case ‘X‘: return 10; 6 case ‘L‘: return 50; 7 case ‘C‘: return 100; 8 case ‘D‘: return 500; 9 case ‘M‘: return 1000; 10 default: return 0; 11 } 12 } 13 class Solution { 14 public: 15 int romanToInt(string s) { 16 17 18 // Start typing your C/C++ solution below 19 // DO NOT write int main() function 20 int result=0; 21 int len=s.length(); 22 result=c2n(s[0]); 23 for(int i =1; i< len; i++) 24 { 25 if(c2n(s[i]) > c2n(s[i-1])) 26 { 27 result +=(c2n(s[i]) - 2*c2n(s[i-1])); 28 } 29 else 30 { 31 result += c2n(s[i]); 32 } 33 } 34 return result; 35 } 36 };
标签:
原文地址:http://www.cnblogs.com/hexhxy/p/4797892.html