# Leetcode | Integer to Roman

Source: Internet
Author: User

• Problem
• Analytical
• Appendix Roman Numeral Spelling Rules

Problem

Integer to roman:https://leetcode.com/problems/integer-to-roman/
Degree:medium

``Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.``
Analytical

Spelling rules for Roman numerals: detailed spelling rules, see the final appendix.
`1, 4, 5, 9, 10, 19 I, IV, V, IX, X, XIX`
Note: 9 can not be written viiii only up to 3 consecutive, also not written VIV, can only be written IX

So we can think of 4 and 9 outstanding.

``#include <string>#include <iostream>using namespace STD;//i (1), V (5), X (10), L (50), C (100), D (500) and M (1000), cannot have 4 consecutiveclassSolution { Public:stringInttoroman (intNUM) {if(Num <1|| num >3999)returnNULL;stringSintd = { +, -,Ten,1}; for(inti =0; num >0&& I <4; i++) {intr = Num/d[i];//There are several            Charc = Inttoromanchar (D[i]);if(r = =0) {Continue; }Else if(R >0&& R <4) { for(intj =0; J < R;            J + +) S.push_back (c); }Else if(r = =4) {S.push_back (c);//Small in frontS.push_back (Inttoromanchar (d[i]*5));//large in the rear}Else if(R >4&& R <9) {S.push_back (Inttoromanchar (d[i]*5));//Large in front                 for(intj =5; J < R; J + +) S.push_back (Inttoromanchar (D[i]));//Small in the rear}Else if(r = =9) {S.push_back (Inttoromanchar (d[i)); S.push_back (Inttoromanchar (d[i-1]));        } num%= d[i]; }returnS }Private:CharInttoromanchar (intD) {Switch(d) { Case 1:return ' I '; Case 5:return ' V '; Case Ten:return ' X '; Case  -:return ' L '; Case  -:return ' C '; Case  -:return ' D '; Case  +:return ' M '; }    }};intMain () {intN Solution Sol; while(1)    {Cin>> N;cout<< Sol.inttoroman (n) << Endl; }}``

According to the above procedure, it can be observed that 4, 9 corresponds to the special, so we can get the following simplified implementation process, equivalent to the expansion of Roman number of spelling rules

``#include <string>#include <iostream>using namespace STD;//i (1), V (5), X (10), L (50), C (100), D (500) and M (1000), cannot have 4 consecutiveclassSolution { Public:stringInttoroman (intNUM) {if(Num <1|| num >3999)returnNULL;stringSConst intD[] = { +, the, -, -, -, -, -, +,Ten,9,5,4,1};Const stringSymbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; for(inti =0; num >0&& I <sizeof(d)/sizeof(d); i++) {intr = Num/d[i]; for(intj =0; J < R;            J + +) s + = Symbol[i];        Num%= d[i]; }returnS }};intMain () {intN Solution Sol; while(1)    {Cin>> N;cout<< Sol.inttoroman (n) << Endl; }}``
Appendix: Roman Numeral spelling Rules

There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000). Any positive integer can be represented by the following rules. It is important to note that there is no "0" in the Roman numerals, regardless of the rounding system. Roman numerals are generally considered to be used only for counting, not for calculation.

• Repeat several times: a Roman number repeats several times, indicating several times the number.
• Right plus left minus:
1. The smaller Roman numerals on the right side of the larger Roman numerals indicate large numbers plus small numbers.
2. The smaller Roman numerals on the left of the larger Roman numerals indicate that large numbers decrease the numbers.
3. The left-minus number is limited to I, X, and C. For example, 45 can not be written as VL, only XLV
4. However, the left subtraction cannot span a single digit. For example, 99 may not be represented by an IC (100-1), but rather by XCIX ([100-10] + [10-1]). (equivalent to each digit of the Arabic numerals.) ）
5. The left minus number must be one, such as 8 written in VIII, not IIX.
6. The right plus number cannot be more than three consecutive digits, such as 14 for XIV rather than XIIII. (See "Digital Restrictions" below.) ）
By adding a horizontal line or a subscript to the Roman numerals, it means multiplying this number by 1000, which is 1000 times times the original number.
Similarly, if there are two horizontal lines above, it is 1000000 (1000^{2}) times the original number.
• Digital Restrictions:
The same digital can only appear at most three times, such as 40 can not be represented as XXXX, but to be represented as XL.
Exception: Since IV is the first word of the Roman mythology Lord Jupiter (ie, ivpiter, the Roman alphabet does not have J and u), it is sometimes substituted with IIII for IV.

Leetcode | Integer to Roman

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.