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

Source: Internet
Author: User

[Link to this article]

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  // Roman2Integer. cpp: Defines the entry point for the console application.
//
/*
Version: 1.0
Author: hellogiser
Blog: http://www.cnblogs.com/hellogiser
Date: 2014/5/26
*/

# Include "stdafx. h"
# Include <string>
# Include <iostream>
# Include <map>
# Include <assert. h>
Using namespace std;

// Roman to integer
Unsigned int roman2integer (string str)
{
// 99 --> 10,100, 1, 10
// 66 ---> 50, 10, 5, 1
If (str = "")
Return 0;
Map <char, int> 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 [0];
Int len = str. length ();
For (int I = 0; I <len-1; I ++)
{
If (m [str [I]> = m [str [I + 1])
{
// M [I]> = m [I + 1], then add m [I + 1] to sum
Sum = sum + m [str [I + 1];
}
Else
{
// M [I] <m [I + 1], then add m [I + 1] to sum, and remove 2 * m [I]
Sum = sum + m [str [I + 1]-2 * m [str [I];
}
}
Return sum;
}

# Deprecision MAX 3999

// Integer to roman
String integer2roman (unsigned int n)
{
// We shoshould consider 400,900, 40, 90
String result = "";
If (n <1 | n> 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 <length; I ++)
{
While (n> = val [I])
{
Result + = r [I];
N-= val [I];
}
}
Return result;
}

// Test case for two functions
Void 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

[Link to this article]

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

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.