Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert integers into Roman numerals in C++

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to convert C++ into Roman numerals". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to convert C++ integers into Roman numerals" can help you solve the problem.

Convert Integer to Roman integers to Roman numerals

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol Value

I 1

V 5

X 10

L 50

C 100

D 500

M 1000

For example, two is written as II in Roman numeral, just two one "s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.

X can be placed before L (50) and C (100) to make 40 and 90.

C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example 1:

Input: 3

Output: "III"

Example 2:

Input: 4

Output: "IV"

Example 3:

Input: 9

Output: "IX"

Example 4:

Input: 58

Output: "LVIII"

Explanation: l = 50, V = 5, III = 3.

Example 5:

Input: 1994

Output: "MCMXCIV"

Explanation: M = 1000, CM = 900,90 and IV = 4.

The previous article is about the conversion of Roman numerals to integer Roman to Integer, this time to integer conversion to Roman numerals, the basic algorithm is still the same. Because the range of input numbers is limited (1-3999), the topic becomes a lot easier.

I-1

V-5

X-10

L-50

C-100

D-500

M-1000

For example, if the Roman numeral of the integer 1437 is MCDXXXVII, it is not difficult to find that the numbers in thousands, hundreds, ten digits and individual digits are represented by Roman numerals respectively. 1000-M400-CD, 30-XXX, 7-VII. So what we need to do is use the quotient method to extract the numbers on each bit, and then express them separately:

100-C

200-CC

300-CCC

400-CD

500-D

600-DC

700-DCC

800-DCCC

900-CM

It can be divided into four categories, 100 to 300, 400, 500 to 800, and 900 to the last. The situation on each bit is similar, and the code is as follows:

Solution 1:

Class Solution {public: string intToRoman (int num) {string res = "; vector roman {" M "," D "," C "," L "," X "," V "," I "}; vector value {1000, 500,100,50,10,5,1}; for (int n = 0; n < 7; n + = 2) {int x = num / value [n] If (x < 4) {for (int I = 1; I 4 & & x < 9) {res + = roman [n-1]; for (int I = 6; I = val [I]) {num-= val [I]; res + = str [I];}} return res }}

The following method is a more opportunistic method, listing all the cases, and then looking up the table directly by bit, the time complexity of O (1), see the code as follows:

Solution 3:

Class Solution {public: string intToRoman (int num) {string res = "; vector v1 {", "M", "MM", "MMM"}; vector v2 {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"} Vector v3 {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; vector v4 {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} Return v1 [num / 1000] + v2 [(num% 1000) / 10] + v3 [(num% 1000) / 10] + v4 [num% 10];}}; that's all for "how C++ converts integers into Roman numerals". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report