Integer to Roman

Source: Internet
Author: User

Given an integer, convert it to a Roman numeral.

Input is guaranteed to being within the range from 1 to 3999.

Question:

Given an integer, convert it to a Roman numeral.

Input is guaranteed to being within the range from 1 to 3999.

Give you an integer, convert it to Roman numerals, and the input is guaranteed to be between 1 and 3999.

About Roman Numerals Introduction:

1, counting method: ① Roman numerals have the following seven basic symbols: Ⅰ (1), Ⅴ (5), Ⅹ (10), L (50), C (100), D (500), M (1000).

② the same number ligatures, the number represented is equal to the number of these numbers added, such as: ⅲ= 3;xx=20;cc=200;mmm=3000;

③ the small number on the right side of the large number, the number represented is equal to the number of these numbers added, such as: ⅷ= 8;ⅻ= 12;

④ small numbers, (limited to Ⅰ, X, and C) on the left side of the large number, the number represented is equal to the number of large number reduction, such as: ⅳ= 4;ⅸ= 9;

⑤ the number of ligatures in normal use shall not exceed three times;

2, group rules: ① basic numbers Ⅰ, X, C any one, the number of their own use, or put on the right side of the large number of the composition of the number, not more than three; on the left side of the large number can only use one.

② can not use any of the basic digits V, L, D as decimals on the left side of the large number of methods to form the number of the subtraction method;

③v and the small number on the left of X can only be used with Ⅰ.

④l and C can only be used on the left side of the small number x.

The small numbers on the left of ⑤d and M can only be used in C.

3, control Example: ① digit example: ⅰ,1 "ⅱ,2" ⅲ,3 "ⅳ,4" ⅴ,5 "ⅵ,6" ⅶ,7 "ⅷ,8" ⅸ,9 "

② 10-bit example: ⅹ,10 "ⅺ,11" ⅻ,12 "xxxv,35" xxxix,39 "xl,40" l,50 "li,51" lv,55 "xc,90" xciii,93 "xcv,95" xcviii,98 "

③ Hundred Examples: c,100 "cc,200" ccc,300 "cd,400" d,500 "dc,600" dcc,700 "dccc,800" cm,900 "cmxcix,999"

④ thousand Examples: m,1000 "mc,1100" mcd,1400 "md,1500" mdc,1600 "mcmxc,1990" mm,2000 "mmmcmxcix,3999

Reference: http://www.cnblogs.com/zhaolizhen/p/romannumber.html

It is not complicated to understand the way of expression of popular science.

I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;

There is a subtraction representation between each of the two stages, such as 900=CM, and C written in front of the M to indicate M-c.

Range to 3999, feel the situation is not more directly hit the table is actually faster, with code to judge the estimate is more cumbersome.

Then is the greedy approach, each time the choice can represent the maximum value, the corresponding string connected together.

Reference: http://blog.csdn.net/havenoidea/article/details/11848921

C + + Implementation code:

#include <iostream>#include<string>using namespacestd;classSolution { Public:    stringInttoroman (intnum) {        intvalue[]={ +, the, -, -, -, -, -, +,Ten,9,5,4,1}; stringstr[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; inti; stringret;  for(i=0;num>0; i++)        {             while(num>=Value[i]) {num-=Value[i]; RET+=Str[i]; }        }        returnret; }};intMain () {solution S; cout<<s.inttoroman (3999) <<Endl;}

Integer to Roman

Contact Us

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.