Computer Programming Contest Preparation

ToolBox - Source for: 123/12397/a.c



/home/toolbox/public_html/solutions/123/12397/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date:
   18  * Purpose: fun
   19  * Problem:
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 int num;
   27 
   28 void init()
   29 {
   30     /* FUNCTION init */
   31 } /* FUNCTION init */
   32 
   33 void dump()
   34 {
   35     /* FUNCTION dump */
   36 } /* FUNCTION dump */
   37 
   38 int getInput()
   39 {
   40     /* FUNCTION getInput */
   41     int dataReadFlag;
   42 
   43     dataReadFlag = (1 == scanf(" %d ", &num));
   44     return (dataReadFlag);
   45 } /* FUNCTION getInput */
   46 
   47 void process()
   48 {
   49     /* FUNCTION process */
   50     int tot;
   51     int tmp;
   52     /* roman numeral values
   53     Symbol  Value Sticks
   54        I      1     1
   55        V      5     2
   56        X     10     2
   57        L     50     2
   58        C    100     2
   59        D    500     3
   60        M  1,000     4
   61     */
   62     /* max value is 3999 */
   63     tot = (num / 1000) * 4; /* each M(1000) needs 4 matches */
   64     num = num % 1000;
   65     if (9 == (num / 100))
   66         {
   67             /* special case 9xx CM */
   68             tot = tot + 6;
   69             num = num - 900;
   70         } /* special case 9xx CM */
   71     tot = tot + (num / 500) * 3; /* each D(500) needs 3 matches */
   72     num = num % 500;
   73     if (4 == (num / 100))
   74         {
   75             /* special case 400 CD */
   76             tot = tot + 5;
   77             num = num - 400;
   78         } /* special case 400 CD */
   79     tot = tot + (num / 100) * 2; /* each C(100) needs 2 matches */
   80     num = num % 100;
   81     if (9 == (num / 10))
   82         {
   83             /* special case 90 XC */
   84             tot = tot + 4;
   85             num = num - 90;
   86         } /* special case 90 XC */
   87     tot = tot + (num / 50) * 2; /* each L(50) needs 2 matches */
   88     num = num % 50;
   89     if (4 == (num / 10))
   90         {
   91             /* special case 40 XL */
   92             tot = tot + 4;
   93             num = num - 40;
   94         } /* special case 40 XL */
   95     tot = tot + (num / 10) * 2; /* each X(10) needs 2 matches */
   96     num = num % 10;
   97     if (9 == num)
   98         {
   99             /* special case 9 IX */
  100             tot = tot + 3;
  101             num = num - 9;
  102         } /* special case 9 IX */
  103     tot = tot + (num / 5) * 2; /* each V(5) needs 2 matches */
  104     num = num % 5;
  105     if (4 == num)
  106         {
  107             /* special case 4 IV */
  108             tot = tot + 3;
  109             num = num - 4;
  110         } /* special case 4 IV */
  111     tot = tot + num * 1; /* each I(1) needs 1 matches */
  112     printf("%d\n", tot);
  113 } /* FUNCTION process */
  114 
  115 int main()
  116 {
  117     /* main */
  118     int moreToDo;
  119 
  120     init();
  121     moreToDo = getInput();
  122     while (moreToDo)
  123         {
  124             /* while */
  125             process();
  126             moreToDo = getInput();
  127         } /* while */
  128 
  129     return EXIT_SUCCESS;
  130 } /* main */
  131