Computer Programming Contest Preparation

ToolBox - Source for: 109/10922/b.c



/home/toolbox/public_html/solutions/109/10922/b.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:
   17  *    Date:
   18  * Purpose:
   19  * Problem:
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 int num;
   27 char buff[1005];
   28 
   29 void init()
   30 {
   31     /* FUNCTION init */
   32 } /* FUNCTION init */
   33 
   34 void dump()
   35 {
   36     /* FUNCTION dump */
   37 } /* FUNCTION dump */
   38 
   39 int getInput()
   40 {
   41     /* FUNCTION getInput */
   42     int dataReadFlag;
   43     buff[0] = '0';
   44     buff[1] = '0';
   45     scanf(" %s ", buff);
   46     if (buff[0] == '0' && buff[1] == 0)
   47         {
   48             dataReadFlag = FALSE;
   49         }
   50     else
   51         {
   52             dataReadFlag = TRUE;
   53         }
   54     return (dataReadFlag);
   55 } /* FUNCTION getInput */
   56 
   57 int recurse(int n)
   58 {
   59     int temp;
   60     if (n <= 9)
   61         {
   62             if ( n == 9)
   63                 {
   64                     return 1;
   65                 }
   66             else
   67                 {
   68                     return -9999;
   69                 }
   70         }
   71     else
   72         {
   73             temp = 0;
   74             while (n != 0)
   75                 {
   76                     temp += n%10;
   77                     n = n/10;
   78                 }
   79             return 1 + recurse(temp);
   80         }
   81 }
   82 void process()
   83 {
   84     /* FUNCTION process */
   85     int dataReadFlag;
   86     int cur = 0;
   87     int degree;
   88     num = 0;
   89     if (buff[0] == '9' && buff[1] == 0)
   90         {
   91             printf("9 is a multiple of 9 and has 9-degree 1.\n");
   92         }
   93     else
   94         {
   95             while (buff[cur] != 0)
   96                 {
   97                     num += (buff[cur] - '0');
   98                     cur++;
   99                 }
  100             degree = recurse(num);
  101             if (degree < 0)
  102                 {
  103                     printf("%s is not a multiple of 9.\n", buff);
  104                 }
  105             else
  106                 {
  107                     printf("%s is a multiple of 9 and has 9-degree %d.\n", buff, degree);
  108                 }
  109         }
  110 
  111 
  112 } /* FUNCTION process */
  113 
  114 int main ()
  115 {
  116     /* main */
  117     int moreToDo;
  118 
  119     init();
  120     moreToDo = getInput();
  121     while (moreToDo)
  122         {
  123             /* while */
  124             process();
  125             moreToDo = getInput();
  126         } /* while */
  127 
  128     return EXIT_SUCCESS;
  129 } /* main */
  130