Computer Programming Contest Preparation

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



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