344 - Roman Digititis

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

valkov
New poster
Posts: 20
Joined: Tue Jul 20, 2010 3:11 pm

Re: 344 Problem

Post by valkov »

Just got AC on this one :)
Here is a neat way to convert decimal to roman numbers:

Code: Select all

const string romanOneToNine[] = {"", "A", "AA", "AAA", "AB", "B", "BA", "BAA", "BAAA", "AC"};
const string romanDigits[] = {"IVX", "XLC", "CDM", "M" };

string GetRomanDigit(unsigned num, unsigned power)
{
    string result = "";
    string digit = romanOneToNine[num];
    for(unsigned i = 0; i < digit.size(); i++) {
        result.push_back(romanDigits[power][digit[i] - 'A']);
	}
    return result;
}

string DecimalToRoman(unsigned num)
{
    unsigned power;
    string result = "";
    for (power = 0; num > 0; power++, num /= 10) {
        result.insert(0, GetRomanDigit(num % 10, power));
    }
    return result;
}

Post Reply

Return to “Volume 3 (300-399)”