1996 Canadian Computing Competition, Stage 1

Problem D: When in Rome...

If the Roman Empire had not fallen, then Rome would surely have discovered electricity and used electronic calculators; however, the Romans used Roman Numerals! Your task is to implement a simple Roman Calculator which accepts two Roman Numerals and outputs the sum in Roman Numerals. You may assume that numbers greater than 1000 will not occur in the input. Output numbers greater than 1000 are illegal and should generate the message CONCORDIA CUM VERITATE (In Harmony with Truth).

The input consists of a number, indicating the number of test cases, followed by this many test cases. Each test case consists of a single line with two numbers in Roman Numerals separated by a + along with an = at the end. There are no separating spaces.

For each test case the output is a copy of the input with the Roman Numeral that represents the sum. Outputs for different test cases are separated by a blank line.

Roman Research

The Roman Numerals used by the Romans evolved over many years, and so there are some variations in the way they are written. We will use the following definitions:

  1. The following symbols are used: I for 1, V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1000.
  2. Numbers are formed by writing symbols from 1. from left to right, as a sum, each time using the symbol for the largest possible value. The symbols M, C, X, or I may be used at most three times in succession. Only if this rule would be violated, you can use the following rule:
    • When a single I immediately precedes a V or X, it is subtracted. When a single X immediately precedes an L or C, it is subtracted. When a single C immediately precedes a D or M, it is subtracted.
For example: II = 2; IX = 9; CXIII = 113; LIV = 54; XXXVIII = 38; XCIX = 99.

Sample Input

3
VII+II=
XXIX+X=
M+I=

Sample Output

VII+II=IX
XXIX+X=XXXIX
M+I=CONCORDIA CUM VERITATE

All Submissions
Best Solutions


Point Value: 10
Time Limit: 2.00s
Memory Limit: 16M
Added: Sep 28, 2008

Problem Types: [Show]

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

When it gives you your output does it clip it to 20 or so characters?

Yes, hence "Your Output (clipped)".

I'm just thinking how it took me an hour to find one typo
The problem is called ccc96 *s4* because it's stage 4 so it's harder?

ccc96s4:

ccc = Canadian Computing Competition
96 = 1996
s = Senior
4 = Problem #4.

The CCC consists only of two stages: the first, open stage; and the second, invitation-only stage, known as the Canadian Computing Olympiad.

CCO used to be known as Stage 2; hence, those problem codes follow the format cccYYs2pN, where YY is the year and N is the problem number.

Bryan's submission gave correct output when I tested it, but fails on the judge.

i bet it's some stupid reason but someone check it?

The rometoint function goes out of bounds

okay so why did it work on mine and yves' computers?

Different compiler and environment might have an effect. (Not too sure about the former.)

The judge compiles with g++ 4.1; I compiled with Bloodshed Dev.

Also, the judge runs programs in a linux environment; we ran it in Windoze.

I can't believe I didn't see that error when I skimmed over your code...

OK, here's a detailed explanation:

Although technically you're going out of bounds, 99% of C++ implementations will have their strings null-terminated - so s[s.size()] will be the null character with value 0, which is fine.

The problem is with the letter() function - if none of the if statements are true (when you call letter(0)), you don't return anything. Your compiler should've warned you about this - if you don't return anything, you're going to get a garbage value that depends on random stuff in memory (which is partly determined by the compiler and the compilation mode).

haha ignored the warning ftw

how should i have fixed it?

Just add a return 0; to the end of your function.
(You could improve the function by using the switch statement instead of an if-elsif-elsif ladder.)


PS: Could we please have an inline monospace text (or even better, inline code) tag?