CCC '12 S2 - Aromatic Numbers

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Problem type

Canadian Computing Competition: 2012 Stage 1, Senior #2

This question involves calculating the value of aromatic numbers which are a combination of Arabic digits and Roman numerals.

An aromatic number is of the form ARARAR \dots AR, where each A is an Arabic digit, and each R is a Roman numeral. Each pair AR contributes a value described below, and by adding or subtracting these values together we get the value of the entire aromatic number.

An Arabic digit A can be 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9. A Roman numeral R is one of the seven letters I, V, X, L, C, D, or M. Each Roman numeral has a base value:

Symbol I V X L C D M
Base value 1 5 10 50 100 500 1000

The value of a pair AR is A times the base value of R. Normally, you add up the values of the pairs to get the overall value. However, wherever there are consecutive symbols ARA^{\prime}R^{\prime} with R^{\prime} having a strictly bigger base value than R, the value of pair AR must be subtracted from the total, instead of being added.

For example, the number 3M1D2C has the value 3 \times 1000 + 1 \times 500 + 2 \times 100 = 3700 and 3X2I4X has the value 3 \times 10 - 2 \times 1 + 4 \times 10 = 68.

Write a program that computes the values of aromatic numbers.

Input Specification

The input is a valid aromatic number consisting of between 2 and 20 symbols.

Output Specification

The output is the decimal value of the given aromatic number.

Sample Input 1

3M1D2C

Output for Sample Input 1

3700

Sample Input 2

2I3I2X9V1X

Output for Sample Input 2

-16

Comments


  • 4
    zsu  commented on Feb. 17, 2018, 12:04 p.m. edited

    Should the statement be "the value of pair AR must be subtracted from the total" instead of "the value of pair A′ must be subtracted from the total"?


  • 7
    msa789  commented on Jan. 6, 2015, 8:02 p.m.

    I don't seem to understand this part: wherever there are consecutive symbols ARAfRf with Rf having a strictly bigger base value than R. What does it mean exactly?


  • -15
    bobhob314  commented on Jan. 5, 2015, 7:30 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.


  • -13
    bobhob314  commented on Jan. 5, 2015, 7:24 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.


  • -13
    bobhob314  commented on Jan. 5, 2015, 7:12 p.m.

    This comment is hidden due to too much negative feedback. Click here to view it.


    • -13
      bobhob314  commented on Jan. 5, 2015, 7:17 p.m.

      This comment is hidden due to too much negative feedback. Click here to view it.


      • -13
        bobhob314  commented on Jan. 5, 2015, 7:30 p.m.

        This comment is hidden due to too much negative feedback. Click here to view it.


        • 1
          noYou  commented on Oct. 2, 2019, 5:47 p.m.

          Delete them