Write a program to convert positive integers into Roman numbers.
The rule for constructing a Roman number is assumed to be as follows.
In Roman number system, i is the symbol for 1, v for 5, x for 10, l for 50,
c for 100, d for 500 and m for 1000.
Symbols with larger values usually appear before symbols with smaller values.
The value of a Roman number is, in general, the sum of the values of the symbols.
For example, ii is 2, viii is 8. However,
if a symbol with smaller value appears before a symbol with larger value,
the value of these two symbols is the difference of the two values.
For example, iv is 4, ix is 9, and lix is 59.
Note that no four consecutive symbols in the Roman number can be the same.
For example, iv, but not iiii, is the Roman number 4.
hint:羅馬數值表,本測資參照http://wywu.pixnet.net/blog/post/23023232。
本題禁止使用迴圈。
The Input
輸入為一個正整數,並且小於100
The Output
根據輸入的整數,輸出對應的羅馬數字
Sample Input
3
Sample Output
III
- #include <stdio.h>
- #include <string.h>
-
- int main()
- {
- char* digits[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
- char* tens[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
- char* hundreds[10] = {"", "C"};
-
- int n, i, j;
-
- while (scanf("%d", &n) != EOF)
- {
- if (n < 1 || n > 100)
- {
- printf("please input between 1~100\n");
- continue;
- }
- printf("%s", hundreds[n%1000/100]);
- printf("%s", tens[n%100/10]);
- printf("%s\n", digits[n%10]);
- }
- system("PAUSE");
- return 0;
- }
Hiç yorum yok:
Yorum Gönder