ProgrammingHardTCS NQT · Accenture

The procedure below converts the Roman numeral "MCMXCIV" to a number by reading left to right and subtracting a symbol's value when the next symbol is larger, otherwise adding it. Symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. What is printed?

The procedure below converts the Roman numeral "MCMXCIV" to a number by reading left to right and subtracting a symbol's value when the next symbol is larger, otherwise adding it. Symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. What is printed?
total = 0
for i = 0 to n - 1
    if i + 1 < n and value(s[i]) < value(s[i+1]) then total = total - value(s[i])
    else total = total + value(s[i])
end for
Print total
  1. A. 1994
  2. B. 2216
  3. C. -1984
  4. D. 1989

Correct answer: A. 1994

MCMXCIV is 1000 + 900 + 90 + 4 = 1994. Adding every symbol gives 2216, reversing the comparison gives -1984, and skipping the last symbol changes the total to 1989.

More Pseudocode and Code Tracing questions

Exams that ask this topic