24h購物| | PChome| 登入
2013-02-27 10:21:57| 人氣1,320| 回應0 | 上一篇 | 下一篇

[UVA][羅馬數字] 12397 - Roman Numerals

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A: Roman Numerals

We would like to build Roman numerals with matches. As you know, Roman numerals are based on the following seven characters: I, V, X, L, C, D, M. Here we introduce the LUSIVERS font, in which the respective characters look like this:

Write a program that counts the number of matches used to build Roman numerals in the LUSIVERS font. This number is exactly the total number of "match heads" in the characters. For instance, to make the number 14 (=XIV), five matches are used.

You must follow the "standard modern Roman numerals" (as shown on the Wikipedia page). Expressions like IC or IIII are not allowed.

Input

Input contains multiple lines, each giving a value of N (1 ≤ N ≤ 3999).

Output

For each test case, output the number of matches required to build the number N in Roman numerals.

Sample Input

14
2011

Sample Output

5
11

Problemsetter: Mak Yan Kei
The LUSIVERS font is available on FontSpace as a freeware

#include <stdio.h>
char* toRoman(int num) {
    const char rcode[13][3] = {"M", "CM", "D", "CD", "C", "XC", "L",
                        "XL", "X", "IX", "V", "IV", "I"};
    const int val[13] = {1000, 900, 500, 400, 100, 90, 50,
                        40, 10, 9, 5, 4, 1};
    char *roman = new char[30], idx = 0;
    int i;
    for(i = 0; i < 13; i++) {
        while(num >= val[i]) {
            num -= val[i];
            roman[idx++] = rcode[i][0];
            if(rcode[i][1] != '\0')
                roman[idx++] = rcode[i][1];
        }
    }
    roman[idx] = '\0';
    return roman;
}
int main() {
    int n, i;
    char cost[128] = {};
    cost['I'] = 1;
    cost['V'] = 2;
    cost['X'] = 2;
    cost['L'] = 2;
    cost['C'] = 2;
    cost['D'] = 3;
    cost['M'] = 4;
    while(scanf("%d", &n) == 1) {
        char *p = toRoman(n);
        n = 0;
        for(i = 0; p[i]; i++)
            n += cost[p[i]];
        printf("%d\n", n);
        delete[] p;
    }
}

台長: Morris
人氣(1,320) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dfs] 12399 - NumPuzz II
此分類上一篇:[UVA][MST] 12507 - Kingdoms

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文