31 Aralık 2012 Pazartesi

羅馬數字轉換


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

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int main()  
  5. {  
  6.  char* digits[10] = {"""I""II""III""IV""V""VI""VII""VIII""IX"};  
  7.  char* tens[10] = {"""X""XX""XXX""XL""L""LX""LXX""LXXX""XC"};  
  8.  char* hundreds[10] = {"""C"};  
  9.   
  10.  int n, i, j;  
  11.   
  12.  while (scanf("%d", &n) != EOF)  
  13.  {  
  14.   if (n < 1 || n > 100)  
  15.   {  
  16.    printf("please input between 1~100\n");  
  17.    continue;  
  18.   }  
  19.   printf("%s", hundreds[n%1000/100]);  
  20.   printf("%s", tens[n%100/10]);  
  21.   printf("%s\n", digits[n%10]);  
  22.  }  
  23.  system("PAUSE");  
  24.  return 0;  
  25. }  

Hiç yorum yok:

Yorum Gönder

Binary Conversion

Problem Description Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary number...