Computer Programming Contest Preparation

ToolBox - Source for: 3/356/j1.c



/home/toolbox/public_html/solutions/3/356/j1.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 #define MAX_SIDE 160
   16 #define ACTUAL 150
   17 
   18 /*
   19  *  Author:
   20  *    Date:
   21  * Purpose:
   22  * Problem: 356
   23  */
   24 
   25 /*
   26  * This template reads data until a terminating value is reached.
   27  */
   28 
   29 int tbl[MAX_SIDE][MAX_SIDE];
   30 int num;
   31 int firstTime = TRUE;
   32 
   33 int init()
   34 {
   35     /* FUNCTION init */
   36     int row;
   37     int col;
   38 
   39     for (row=0; ACTUAL>=row; row++)
   40         {
   41             /* for each row */
   42             for (col=0; ACTUAL>=col; col++)
   43                 {
   44                     /* for each column */
   45                     tbl[row][col] = row*row + col*col;
   46                 } /* for each column */
   47         } /* for each row */
   48 } /* FUNCTION init */
   49 
   50 int dump()
   51 {
   52     /* FUNCTION dump */
   53 } /* FUNCTION dump */
   54 
   55 int getInput()
   56 {
   57     /* FUNCTION getInput */
   58     int dataReadFlag;
   59 
   60     dataReadFlag = (0 < scanf(" %d ", &num));
   61     return (dataReadFlag);
   62 } /* FUNCTION getInput */
   63 
   64 void process()
   65 {
   66     /* FUNCTION process */
   67     int in = 0;
   68     int on = 0;
   69     int row;
   70     int col;
   71     /* vertices */
   72     double s;
   73 
   74     on = 4 * ((2 * num) - 1);
   75     s = (num - 0.5) * (num -0.5);
   76     for (row=0; num>row; row++)
   77         {
   78             /* for each row */
   79             for (col=0; num>col; col++)
   80                 {
   81                     /* for each column */
   82                     if (s >= tbl[row][col])
   83                         {
   84                             /* square is on circle or inside */
   85                             if (s > tbl[row+1][col+1])
   86                                 {
   87                                     /* inside circle */
   88                                     in++;
   89                                 } /* inside circle */
   90                         } /* square is on circle or inside */
   91                 } /* for each column */
   92         } /* for each row */
   93     if (! firstTime)
   94         {
   95             printf("\n");
   96         }
   97     else
   98         {
   99             firstTime = FALSE;
  100         }
  101     printf("In the case n = %d, %d cells contain segments of the circle.\n", num, on*4);
  102     printf("There are %d cells completely contained in the circle.\n", in*4);
  103 } /* FUNCTION process */
  104 
  105 int main ()
  106 {
  107     /* main */
  108     int moreToDo;
  109 
  110     init();
  111     moreToDo = getInput();
  112     while (moreToDo)
  113         {
  114             /* while */
  115             process();
  116             moreToDo = getInput();
  117         } /* while */
  118 
  119     return EXIT_SUCCESS;
  120 } /* main */
  121