# 58. Analysis, testing and summary: Conversion of roman and Arabic numerals [roman to integer and integer to roman in c ++]

Source: Internet
Author: User

Http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html

Question]

Returns a roman number and converts it to an Arabic number. This question only considers the number less than 3999.

The Roman numerals have the following symbols:

I (1) V (5) x (10) L (50) C (100) D (500) M (1000)

Counting rules:

(1). Several numbers in the same number are the sum of these Roman numerals, for example, III = 3;

(2) A small number represents a number before a large number, that is, a large number minus a small number, for example, IV = 4;

(3). A small number after a large number represents a large number plus a small number, such as VI = 6;

Combination rules:

(1) No more than three of the basic numbers I, X, and C can be used as the number or placed on the right of the large number; you can only use one value to the left of a large number.

(2) do not place any of the basic numbers V, L, and D as decimal places on the left of a large number and subtract them to form the number. add them to the right of a large number to form the number, only one instance can be used.

(3) the small numbers on the left of V and X can only be I.

(4) numbers on the left of L and C can only be X.

(5) the small numbers on the left side of D and M can only use C.

Analysis]

(1) Conversion of Roman numerals to Arabic numerals:

Traverse the roman numerals from the beginning to the end. If a number is smaller than the previous one, add the number to the result. Otherwise, subtract the previous number twice in the result and add the current number;

For example, how does one obtain XVIII = 18? The corresponding Arabic number is 10_5_1_1_1, so the result is 10 + 5 + 1 + 1 + 1 = 18;

How do I get XIX = 19? The corresponding Arabic number is 10_000010, so the result is 10 + 1 + 10-2*1 = 19.

(2) Arabic numerals to Roman numerals:

Use the combination of all small numbers as the basic number to create a corresponding value ing table.

For example, 4 = 1-5 = IV, 9 = 1-10 = IX, 40 = 10-50 = XL, 90 = 10-100 = XC, 400 = 100-500 = CD, 900 = 100-1000 = CM.

The corresponding ing is as follows:

Unsigned int val [] = {1000,900,500,400,100, 90, 50, 9, 5 };

String r [] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL ", "X", "IX", "V", "IV", "I "};

For the Arabic number n, traverse the val array. If n> = val [I], the result is retained by r [I] and n = n-val [I], until n = 0.

Test]

Given a number n, use the integer2raman function to convert it to a number r, and then use the roman2integer function to convert r to m. If n! = M, it indicates that the function is faulty. If they are equal, the function is correct.

[Code]

C ++ Code
 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 // Roman2Integer. cpp: Defines the entry point for the console application.///*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate: 2014/5/26*/# Include "stdafx. h"# Include # Include # Include # Include Using namespace std;// Roman to integerUnsigned int roman2integer (string str){// 99 --> 10,100, 1, 10// 66 ---> 50, 10, 5, 1If (str = "")Return 0;Map m;M ['I'] = 1;M ['V'] = 5;M ['X'] = 10;M ['l'] = 50;M ['C'] = 100;M ['d] = 500;M ['M'] = 1000;Int sum = m [str ;Int len = str. length ();For (int I = 0; I = m [str [I + 1]){// M [I]> = m [I + 1], then add m [I + 1] to sumSum = sum + m [str [I + 1];}Else{// M [I] MAX)Return result;Unsigned int val [] ={ 1000,900,500,400,100, 90, 50, 40, 10, 9, 5, 4, 1 };Unsigned int length = sizeof (val)/sizeof (int );String r [] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL ", "X", "IX", "V", "IV", "I "};For (int I = 0; I = val [I]){Result + = r [I];N-= val [I];}}Return result;}// Test case for two functionsVoid test_case (int n){For (int I = 1; I <= n; I ++){String roman = integer2roman (I );Int integer = roman2integer (roman );Assert (I = integer );}}Void test_main (){// Test_case (20 );Test_case (MAX );}Int _ tmain (int argc, _ TCHAR * argv []){Test_main ();Return 0;}

The implementation of roman2integer and integer2roman is given above, and the function is tested. For the number n between 1 and 3999, obtain the corresponding Roman number as r, and then convert r to the Arabic number m, then n should be equal to m. Therefore, the assert (I = integer); statement in test_case can run normally without throwing an exception.

[Reference]

Http://www.cnblogs.com/dosxp/archive/2008/08/13/1266781.html

Http://blog.csdn.net/wzy_1988/article/details/17057929

Http://blog.csdn.net/fightforyourdream/article/details/12934139

Http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html

Related Keywords:
Related Article

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.