First, let's briefly introduce the Roman numerals. Seven roman numerals are taken from Wikipedia: I (1), V (5), X (10), L (50), and C (100) D (500), and M (1000 ). Any positive integer can be expressed according to the following rules. It should be noted that there is no "0" in the number of Rome, and it has nothing to do with the carry system. Generally, it is considered that a number in Rome is only used for counting, rather than computing. Repeat several times: If a roman number is repeated several times, it indicates several times the number. Right plus left minus: on the right side of a larger roman number, a smaller roman number indicates a larger number and a smaller number. On the left side of a large roman number, a small roman number is displayed, indicating that a large number reduces the number. There is a limit on the number to be subtracted from the left, which is limited to I, X, and C. For example, 45 cannot be written as VL, but XLV can only be used. However, the value of left subtraction cannot span one digit. For example, 99 cannot be represented by IC (100-1), but XCIX ([100-10] + [10-1. (Equivalent to Arabic numerals, each digit is represented separately .) The left minus number must be one digit. For example, 8 is written as VIII, not IIX. The right-plus number cannot exceed three consecutive digits. For example, 14 is written as XIV rather than XIIII. (See "Digital restrictions" below .) Add a line to multiply thousands: Add a horizontal line above the number of Rome or add a lower mark to multiply this number by 1000, that is, 1000 times the original number. Similarly, if there are two crosslines above, that is, 1000000 (1000 ^ {2}) times of the original number. Digital restrictions: the same number can only appear three times at most, for example, 40 cannot be expressed as XXXX, but to express as XL. Exception: Because IV is the first character of the ancient Rome mythical master God Zhu Piter (IVPITER, which has no J and U in the ancient Rome letters), IIII is sometimes used to replace IV. Roman to IntegerGiven a roman numeral, convert it to an integer. input is guaranteed to be within the range from 1 to 3999. the Roman numerals in the range of 3999 do not start with the last character using a underlined letter. If the number corresponding to the current character is smaller than the previous one, the result is subtracted from the number corresponding to the current character, otherwise, add the number corresponding to the current character. To handle the boundary condition, add a character at the end of the original string, which is the original tail character. 1234567891011121314151617 class Solution {public: int romanToInt (string s) {int map [26]; map ['I'-'a'] = 1; map ['V'-'a'] = 5; map ['X'-'a'] = 10; map ['l'-'a'] = 50; map ['C'-'a'] = 100; map ['D'-'a'] = 500; map ['M'-'a'] = 1000; int res = 0, n = s. size (); s. push_back (s [n-1]); for (int I = 0; I <n; I ++) {if (map [s [I]-'a']> = map [s [I + 1]-'a']) res + = map [s [I]-'a']; else res-= map [s [I]-'a'];} return res; }; Integer to Roman Given an integer, convert it to a roman numeral. input is guaranteed to be within the range from 1 to 3999 we noticed that the letters of the Roman numerals are regular and can be divided into several groups, I (1), V (5) is a group, X (10), L (50) is a group, C (100), D (500) is a group, M (1000 ), d (it should be D plus an upper line, indicating 5000) is a group ....... The two numbers in the latter group are 10 times that in the former group. For an integer greater than 10, we represent this Integer as a Roman number by bit. The number 1 ~ 9 respectively: I II III IV V VI VII VIII IX 10 digits 1 ~ 9. Replace I with X, V with L, and X with C, that is, 1 ~ 9 indicates 10 ~ 90. Hundreds of places, thousands of places, and so on ...... 1234567891011121314151617181920212223242526272829303132333435363738 class Solution {public: string intToRoman (int num) {char romanChar [] = {'I', 'V', 'x', 'l', 'C ', 'D', 'M'}; string res; int I = 6, factor = 1000; while (num! = 0) {helper (num/factor, & romanChar [I], res); I-= 2; num % = factor; factor/= 10;} return res ;} void helper (int k, char romanChar [], string & res) {// 0 <= k <= 9 if (k <= 0 ); else if (k <= 3) res. append (k, romanChar [0]); else if (k = 4) {res. push_back (romanChar [0]); res. push_back (romanChar [1]);} else if (k <= 8) {res. push_back (romanChar [1]); res. append (K-5, romanChar [0]);} else if (k = 9) {res. push_back (romanChar [0]); res. push_back (romanChar [2]) ;}};