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:
- The following symbols are used:
I
for 1,V
for 5,X
for 10,L
for 50,C
for 100,D
for 500, andM
for 1000. - 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
, orI
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 aV
orX
, it is subtracted. When a singleX
immediately precedes anL
orC
, it is subtracted. When a singleC
immediately precedes aD
orM
, it is subtracted.
- When a single
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
Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3
Comments (Search)
The problem is called ccc96 *s4* because it's stage 4 so it's harder?
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.
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...
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).
how should i have fixed it?
(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?