Hdoj/hdu 2352 Verdis Quo (conversion of Roman numerals to 10 binary numbers)

Source: Internet
Author: User
Tags what numeric value

Problem Description
The Romans used letters from their Latin alphabet to represent each of the seven numerals in their number system. The list below shows which
Letters they used and what numeric value each of those letters represents:

I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000

Using These seven numerals, any desired number can is formed by following the both basic additive and subtractive rules. To form a number using
The additive rule the Roman numerals is simply written from left to right in descending order, and the value of each Roma N Numeral is added
Together. For example, the number MMCLVII has the value 1000 + 1000 + 100 + 50 + 5 + 1 + 1 = 2157. Using The addition rule alone could leads to
Very long strings of letters, so the subtraction rule was invented as a result. Using This rule, a smaller Roman numeral to the left of a larger one is
subtracted from the total. In other words, the number mcmxiv is interpreted as 1000-100 + 1000 + 10-1 + 5 = 1914.

Over time the Roman number writing system became more standardized and several additional rules were developed. The additional rules used today
is:

    1. The I, X, or C Roman numerals May is repeated up to three times in succession. In other words, the number 4 must is represented as IV
      And not as IIII.
    2. The V, L, or D numerals may never is repeated in succession, and the M numeral could be repeated as many 2. times as necessary.
    3. Only one smaller numeral can is placed to the left of another. For example, the number is represented as XVIII and not as Xiix.
    4. Only the I, X, or C can be used as subtractive numerals.
    5. A subtractive I can only is used to the left of a V or X likewise a X can is appear to the left of a L or C, and a C CA n is used to the
      Left of a D or M. For example, must is written as XLIX and not as IL.

Your goal is to write a program which converts Roman numbers to base integers.

Input
The input to this problem would consist of the following:

A line with a single integer "N" (1≤n≤1000), where N indicates how many Roman numbers is to be converted.
A series of N lines of input with each line containing one Roman number. Each Roman number is in the range of 1 to (inclusive)
And would obey all of the rules laid out in the problem ' s introduction.

Output
For each of the N Roman numbers, print the equivalent base integer, one per line.

Sample Input
3
Ix
Mmdcii
Dxii

Sample Output
9
2602
512


There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500) and M (1000).
1, repeated several times: a Roman numeral repeated several times, it means that the number of several times.
2. Right plus left minus:
2.1 The smaller Roman numerals on the right side of the larger Roman numerals, indicating large numbers plus small numbers.
2.2 The smaller Roman numerals on the left of the larger Roman numerals, indicating that large numbers decrease the numbers.
2.3 The left minus number is limited to I, X, C. For example, 45 can not be written as VL, only XLV
2.4 However, the left minus time cannot span one digit. For example, 99 may not be represented by an IC (100-1), but rather by XCIX ([100-10] + [10-1]). (equivalent to each digit of the Arabic numerals.) )
2.5 The left minus number must be one, such as 8 written in VIII, not IIX.


Note: I can only be on the left side of V,x. X is only on the left side of L,c. C can only be on the left side of D,m.
Knowing this can be AC.

ImportJava.util.Scanner;/** * @author Chen Haoxiang * 2016-6-5 * * Public  class Main{    Static Char[] chs={' I ',' V ',' X ',' L ',' C ',' D ',' M '};Static int[] chn={1,5,Ten, -, -, -, +};StaticString[] strs={"IV","IX","XL","XC","CD","CM"};Static int[] strn={2,2, -, -, $, $}; Public Static void Main(string[] args) {Scanner SC =NewScanner (system.in);intT=sc.nextint (); while(t-->0) {String str=sc.next ();intnum=0; for(intI=0; I<str.length (); i++) { for(intj=0; j<chs.length;j++) {if(Str.charat (i) ==chs[j]) {NUM+=CHN[J]; Break; }}} String s=""; for(intI=1; I<str.length (); i++) {s=""+str.charat (I-1) +str.charat (i); for(intj=0; j<strs.length;j++) {if(S.equals (Strs[j])) {NUM-=STRN[J]; Break;        }}} System.out.println (num); }    }}

Hdoj/hdu 2352 Verdis Quo (conversion of Roman numerals to 10 binary numbers)

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.