Computer Programming Contest Preparation

ToolBox - Source for: 3/382/j.c



/home/toolbox/public_html/solutions/3/382/j.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: 2015-02-24
   18  * Purpose:
   19  * Problem: 382
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_SIZE 60001
   27 
   28 int num;
   29 int ans[MAX_SIZE];
   30 char answer[4][10] = {"PERFECT", "DEFICIENT", "ABUNDANT", "INVALID"};
   31 
   32 int sum(int tmp)
   33 {
   34     /* FUNCTION sum */
   35     int i;
   36     int stp;
   37     int tot=1;
   38 
   39     stp = sqrt(tmp+1);
   40     for (i=2; i<=stp; i++)
   41         {
   42             /* for */
   43             DEBUG printf("Trying tmp(%d) %d\n", tmp, i);
   44             if (0 == (tmp % i))
   45                 {
   46                     /* found a divisor */
   47                     DEBUG printf("tot(%d) tmp(%d) i(%d) %d\n", tot, tmp, i, (tmp/i));
   48                     tot = tot + i + (tmp / i);
   49                 } /* found a divisor */
   50         } /* for */
   51     return tot;
   52 } /* FUNCTION sum */
   53 
   54 void init()
   55 {
   56     /* FUNCTION init */
   57     int i;
   58     int tot;
   59 
   60     DEBUG printf("ans[%d] = [%s]\n", 0, answer[0]);
   61     DEBUG printf("ans[%d] = [%s]\n", 1, answer[1]);
   62     DEBUG printf("ans[%d] = [%s]\n", 2, answer[2]);
   63     DEBUG printf("ans[%d] = [%s]\n", 3, answer[3]);
   64 
   65     ans[0]=3;
   66     ans[1]=1;
   67     ans[2]=1;
   68     ans[3]=1;
   69     for (i=4; MAX_SIZE>i; i++)
   70         {
   71             /* for */
   72             tot=sum(i);
   73             DEBUG printf("sum of %d is %d\n", i, tot);
   74             if (tot == i)
   75                 {
   76                     /* perfect */
   77                     ans[i] = 0;
   78                 } /* perfect */
   79             else if (0 < (i-tot))
   80                 {
   81                     /* deficient */
   82                     ans[i] = 1;
   83                 } /* deficient */
   84             else
   85                 {
   86                     /* abundant */
   87                     ans[i] = 2;
   88                 } /* abundant */
   89         } /* for */
   90 } /* FUNCTION init */
   91 
   92 void dump()
   93 {
   94     /* FUNCTION dump */
   95 } /* FUNCTION dump */
   96 
   97 int getInput()
   98 {
   99     /* FUNCTION getInput */
  100     int dataReadFlag;
  101 
  102     scanf(" %d ", &num);
  103     dataReadFlag = 0 != num;
  104     return (dataReadFlag);
  105 } /* FUNCTION getInput */
  106 
  107 void process()
  108 {
  109     /* FUNCTION process */
  110     int tot;
  111 
  112     printf("%5d  %s\n", num, answer[ans[num]]);
  113 } /* FUNCTION process */
  114 
  115 int main()
  116 {
  117     /* main */
  118     int moreToDo;
  119 
  120     init();
  121     moreToDo = getInput();
  122     printf("PERFECTION OUTPUT\n");
  123     while (moreToDo)
  124         {
  125             /* while */
  126             process();
  127             moreToDo = getInput();
  128         } /* while */
  129     printf("END OF OUTPUT\n");
  130 
  131     return EXIT_SUCCESS;
  132 } /* main */
  133