I request you to please read this before marking as Duplicate.
I have referred to below:
After understanding, I have designed following algorithm to convert roman to decimal.
- Let Roman String be ......
Replace string contents of length = 4 with appropriate content
- Length = 4, Roman numerals are
VIII = 8
- Length = 4, Roman numerals are
Replace string contents of length = 3 with appropriate content
- Length = 3, Roman numerals are
MMM = 3000,XXX = 30,VII = 7,III = 3
- Length = 3, Roman numerals are
- Replace string contents of length = 2 with appropriate content
- Length = 2, Roman numerals are
MM = 2000,XX = 20,XI = 11,IX = 9,VI = 6,IV = 4,II = 2
- Length = 2, Roman numerals are
- Once this is done, then follow below rules:
- Subtract digits from each other
Ican be subtracted (absolute) fromVorXXcan be subtracted (absolute) fromLorCCcan be subtracted (absolute) fromDorM
- Finally there will be a bunch of digits. Add all of them to get the answer
- Subtract digits from each other
My questions are :
- Is the algorithm correct ?
- I am planning to optimize the algorithm. Does anyone have any tips ?
length 4with appropriate content. Strings of length four as per algo areVIII = 8, no such string found in input string. Next it will move to replacelength 3. Again no string of length three found in input. Next it will move to replacelength 2strings.MMandIVfound, replaced with numbers2000and4. So input now is2000 CMXC 4. Now move with subtraction.C-M[100 - 1000] andX-C[10-100] all absolute. So input string is now2000 900 90 4. Finally algo says add them all. So we get 2994 – SimpleGuy Sep 02 '16 at 02:38