In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how C++ converts Roman numerals into integers". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how C++ converts Roman numerals into integers.
Roman to Integer Roman numerals converted to integers
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 a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: "III"
Output: 3
Example 2:
Input: "IV"
Output: 4
Example 3:
Input: "IX"
Output: 9
Example 4:
Input: "LVIII"
Output: 58
Explanation: l = 50, V = 5, III = 3.
Example 5:
Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900,90 and IV = 4.
Roman numerals are converted into numbers, and we need to be familiar with Roman numerals to complete the conversion. The following is taken from Baidu encyclopedia:
Roman numeral is the earliest way of expressing numbers, which is more than 2000 years earlier than Arabic numerals and originated in Rome.
Today our most common Roman numerals are the dial symbols of watches and clocks: Ⅰ, Ⅱ, Ⅲ, Ⅳ (IIII), Ⅴ, Ⅵ, Ⅶ, Ⅷ, Ⅸ, Ⅹ, Ⅺ, Ⅻ.
Corresponding to Arabic numerals (which are now commonly used internationally), they are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. (note: Arabic numerals were actually invented by ancient Indians and later introduced into Europe by Arabs and were mistakenly called Arabic numerals by Europeans. )
I-1
V-5
X-10
L-50
C-100
D-500
M-1000
1. The same number is concatenated, and the number represented is equal to the number obtained by the sum of these numbers, such as: Ⅲ = 3
2. The small number is to the right of the large number, and the number represented is equal to the number obtained by the sum of these numbers, such as: Ⅷ = 8; Ⅻ = 12
3. A small number (limited to Ⅰ, X, and C) on the left side of a large number, the number represented is equal to the number obtained by a large number minus a decimal, such as Ⅳ = 4; Ⅸ = 9
4. In normal use, the consecutive numbers shall not be repeated more than three times. (except for 04:00 "IIII" on the dial)
5. Draw a horizontal line above a number, indicating that the number has increased by 1000 times.
There are a few points to pay attention to:
1. No one of the basic numbers Ⅰ, X, C can be used to form a number itself or on the right side of a large number, but only one can be used on the left side of a large number.
2. No one of the basic numbers V, L, D can be put as a decimal on the left side of a large number to form a number by subtraction; on the right side of a large number, only one can be used.
The small digits on the left of 3, V, and X can only be used in Ⅰ.
The small numbers on the left of 4, L and C can only use X.
5. The small digits on the left of D and M can only be used in C.
The good thing about this question is that we are not allowed to verify whether the input string is a Roman numeral, which saves a lot of effort. You need to use the HashMap data structure to convert the letters of Roman numerals into corresponding integer values, because you must be entering Roman numerals, so you only need to consider two situations:
First, if the current number is the last number, or if the subsequent number is smaller than it, add the current number.
Second, in other cases, subtract this figure.
Solution 1:
Class Solution {public: int romanToInt (string s) {int res = 0; unordered_map m {{"I", 1}, {"V", 5}, {"X", 10}, {"L", 50}, {"C", 100}, {"D", 500}, {"M", 1000}}; for (int I = 0; I < s.size () + + I) {int val = m [s [I]; if (I = = s.size ()-1 | | m [I + 1]]
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.