Computer Programming Contest Preparation

ToolBox - Source for: 104/10487/a.c



/home/toolbox/public_html/solutions/104/10487/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: 2019-01-29
   18  * Purpose: fun
   19  * Problem: 10487
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_LIST 1005
   27 #define MAX_QUERY 100
   28 
   29 int caseCnt = 1;
   30 int cnt;
   31 int list[MAX_LIST];
   32 int qCnt;
   33 int query[MAX_QUERY];
   34 int best[MAX_QUERY];
   35 int diff[MAX_QUERY];
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45     int i;
   46 
   47     printf("List members: %d\n", cnt);
   48     for (i=0; cnt>i; i++)
   49         {
   50             /* for */
   51             printf("%3d: %d\n", i, list[i]);
   52         } /* for */
   53     printf("Query members: %d\n", qCnt);
   54     for (i=0; qCnt>i; i++)
   55         {
   56             /* for */
   57             printf("%3d: %d\n", i, query[i]);
   58         } /* for */
   59     printf("\n");
   60 } /* FUNCTION dump */
   61 
   62 int getInput()
   63 {
   64     /* FUNCTION getInput */
   65     int dataReadFlag;
   66     int i;
   67 
   68     scanf(" %d ", &cnt);
   69     dataReadFlag = (0 != cnt);
   70     if (dataReadFlag)
   71         {
   72             /* get data */
   73             for (i=0; cnt>i; i++)
   74                 {
   75                     /* for */
   76                     scanf(" %d ", &list[i]);
   77                 } /* for */
   78             scanf(" %d ", &qCnt);
   79             for (i=0; qCnt>i; i++)
   80                 {
   81                     /* for */
   82                     scanf(" %d ", &query[i]);
   83                     best[i] = list[0] + list[1];   /* make a first guess */
   84                     diff[i] = abs(query[i] - best[i]);
   85                 } /* for */
   86         } /* get data */
   87     return (dataReadFlag);
   88 } /* FUNCTION getInput */
   89 
   90 int intcmp(const void *v1, const void *v2)
   91 {
   92     /* FUNCTION intcmp */
   93     return (*(int *)v1 - *(int *)v2);
   94 } /* FUNCTION intcmp */
   95 
   96 
   97 void process()
   98 {
   99     /* FUNCTION process */
  100     int i;
  101     int j;
  102     int q;
  103     int sum;
  104     int tmp;
  105 
  106     DEBUG dump();
  107     DEBUG qsort(list, cnt, sizeof(list[0]), intcmp);
  108     /* now hunt for answers */
  109     for (i=0; (cnt - 1) > i; i++)
  110         {
  111             /* lower number */
  112             for (j=i+1; cnt > j; j++)
  113                 {
  114                     /* upper number */
  115                     sum = list[i] + list[j];
  116                     for (q=0; qCnt>q; q++)
  117                         {
  118                             /* check against each query */
  119                             tmp = abs(query[q] - sum);
  120                             if (tmp < diff[q])
  121                                 {
  122                                     /* found a better answer */
  123                                     diff[q] = tmp;
  124                                     best[q] = sum;
  125                                 } /* found a better answer */
  126                         } /* check against each query */
  127                 } /* upper number */
  128         } /* lower number */
  129     for (q=0; qCnt > q; q++)
  130         {
  131             /* dump results */
  132             printf("Closest sum to %d is %d.\n", query[q], best[q]);
  133         } /* dump results */
  134 } /* FUNCTION process */
  135 
  136 int main()
  137 {
  138     /* main */
  139     int moreToDo;
  140 
  141     init();
  142     moreToDo = getInput();
  143     while (moreToDo)
  144         {
  145             /* while */
  146             printf("Case %d:\n", caseCnt);
  147             caseCnt++;
  148             process();
  149             moreToDo = getInput();
  150         } /* while */
  151 
  152     return EXIT_SUCCESS;
  153 } /* main */
  154