I. Title Description
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Two. Topic analysis
Roman Numerals Summary:
1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
10~90: {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
100~900: {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
1000~3000: {"M", "MM", "MMM"}.
Reference to the Roman numeral: http://baike.baidu.com/view/1246899.htm
The problem is to enter an Arabic numeral and convert it to a Roman numeral string.
The problem can be manipulated using the simplest operation, which takes the low to high position of the number. Take one bit at a time, take one and then the number is 10, and the number taken out is expressed in Roman numerals. It is important to note that if the current bit is not the original number, you need to expand 10 times times or 100 times times.
Three. Sample code
Class Solution { Public:string Inttoroman(intNUM) {stringStrstringsymbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};int value[]= { +, the, -, -, -, -, -, +,Ten,9,5,4,1}; for(intI=0; num!=0; ++i) { while(num>=value[i]) {num-=value[i]; Str+=symbol[i]; } }returnStr }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode notes: Integer to Roman