程式語言 - LeetCode - C - 12. Integer to Roman



參考資訊:
https://algo.monster/liteproblems/12

題目:


解答:

char* intToRoman(int num)
{
    char *r = calloc(255, sizeof(char));
    char* str[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I", NULL };
    int dec[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };

    for (int i = 0; str[i] != NULL; i++) {
        while (num >= dec[i]) {
            num -= dec[i];
            strcat(r, str[i]);
        }
    }

    return r;
}