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 realize the Transformation of Roman numerals

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to realize the transformation of Roman numerals". In the daily operation, I believe that many people have doubts about how to realize the transformation of Roman numerals. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to realize the transformation of Roman numerals"! Next, please follow the editor to study!

Problem description

Roman numerals contain the following seven characters: I, V, V, X, L, L, C, D and M.

Character value

I 1

V 5

X 10

L 50

C 100

D 500

M 1000

For example, the Roman numeral 2 is written as II, that is, two juxtaposed ones. 12 write as XII, that is, X+II. 27 write as XXVII, that is, XX+V+II.

Usually, the small number of the Roman numeral is to the right of the large number. However, there are special cases, for example, 4 is not written as IIII, but IV. The number 1 is to the left of the number 5, and the number represented is equal to the number 4 obtained by the large number 5 minus the decimal 1. Similarly, the number 9 is represented as IX. This special rule applies only to the following six situations:

I can be placed on the left side of V (5) and X (10) to represent 4 and 9

X can be placed to the left of L (50) and C (100) to represent 40 and 90.

C can be placed on the left side of D500and M (1000) to represent 400and 900s.

Example 1:

Enter: 3

Output: "III"

Explanation: there are three "I" added together

Example 2:

Enter: 4

Output: "IV"

Example 3:

Enter: 58

Output: "LVIII"

Solution

When there is a list of Roman numerals in the title, the first thing to think of is the dictionary, because the conditional relationship between value and key in the dictionary can be used for the transformation of Roman numeral characters, so you can get a set of dictionaries, so go back to the topic, and you can find that the corresponding value of each Roman character is a positive integer and can be divisible by 5 (except 1), then you can think about it. After entering a numeric value, whether the conversion character can be converted by the way of integral division, for example, when entering a numeric value: 21, naturally its Roman character is XXI, observe and think carefully, you can think that it is divisible by 10 to get 2 XX 2, that is, two XXI, the remaining 1 can be divisible by I to get an I, the two merge each other, and the resulting XXI, such a thought. It can be concluded that the number divided by the maximum value in the dictionary is the corresponding number of Roman characters, and the problem will be easily solved.

Conversion Python Code of Roman numerals

Num=int (input ())

Dict_hs= {1000 langlu mechatron 900lc pr 500lv donglu 400lv lcturo 100lv langzu 90lv XC lavador50 Lang lu 40lv XLZ 10lx lxtra lxtra 5 vermicelli Velcro 4 IVS lvinci

Res=''

Forkeyindict_hs:

Ifnum _ scoop _ keyboards 0:

Count=num//key

Res+=dict_hs [key] * count

Num-=key*count

Print (res)

The idea of solving the problem in the above code is mainly based on the hash table, which is also the idea of most scholars, but the boss in the comment area of this question impressed me and had a different idea on this problem. One boss said that the hash table can be replaced with a list. The general idea is to create two lists, the elements in the list correspond to Roman characters one by one, and use for loop to traverse the length. Then through the subscript to solve the problem, the idea of solving the problem is very wonderful.

Num=int (input ())

N = ['Mauremenagronomy' CMaumanmento 'Dilippagrons' Cedars pr é cors' XCunes pundits' Lobblems' XLengpi 'XLunqiang' XXyunqiang 'IXyunqiang' Variations''

N = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]

Ans=''

Foriinrange (len (n)):

Ifnum > = n [I]:

Count=num//n [i]

Num-=n [I] * count

Ans+=N [I] * count

Print (ans)

Run the example:

At this point, the study of "how to achieve the transformation of Roman numerals" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report