Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/104/10474/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: 2016-03-29
   18  * Purpose: fun
   19  * Problem: 10474 - Where is the marble?
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAXX 10004
   27 
   28 int cnt;
   29 int marbles[MAXX];
   30 int query;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35 } /* FUNCTION init */
   36 
   37 void dump()
   38 {
   39     /* FUNCTION dump */
   40     int i;
   41 
   42     for (i=0; i<cnt; i++)
   43         {
   44             printf("%d ", marbles[i]);
   45         }
   46     printf("\n");
   47 } /* FUNCTION dump */
   48 
   49 int compare(const void *a, const void *b)
   50 {
   51     /* FUNCTION compare */
   52     return ( *(int*)a - *(int*)b );
   53 } /* FUNCTION compare */
   54 
   55 int getInput()
   56 {
   57     /* FUNCTION getInput */
   58     int dataReadFlag = FALSE;
   59     int i;
   60     int tmp;
   61 
   62     scanf(" %d %d ", &cnt, &query);
   63     if ((0 != cnt) || (0 != query))
   64         {
   65             /* something to do  */
   66             dataReadFlag = TRUE;
   67             for (i=0; i<cnt; i++)
   68                 {
   69                     /* for */
   70                     scanf(" %d ", &marbles[i]);
   71                 } /* for */
   72             DEBUG dump();
   73             qsort(marbles, cnt, sizeof(int), compare);
   74             DEBUG dump();
   75         } /* something to do  */
   76 
   77     return (dataReadFlag);
   78 } /* FUNCTION getInput */
   79 
   80 int srch(int x)
   81 {
   82     /* FUNCTION srch */
   83     int i;
   84     int found = -1;
   85 
   86     for (i=0; i<cnt; i++)
   87         {
   88             /* for */
   89             if (x == marbles[i])
   90                 {
   91                     /* found it */
   92                     found = i;
   93                     i = cnt;
   94                 } /* found it */
   95         } /* for */
   96     return found;
   97 } /* FUNCTION srch */
   98 
   99 void process()
  100 {
  101     /* FUNCTION process */
  102     int i;
  103     int num;
  104     int fnd;
  105 
  106     for (i=0; i<query; i++)
  107         {
  108             /* for each query */
  109             scanf(" %d ", &num);
  110             fnd = srch(num);
  111             if (-1 == fnd)
  112                 {
  113                     /* not found */
  114                     printf("%d not found\n", num);
  115                 } /* not found */
  116             else
  117                 {
  118                     /* found */
  119                     printf("%d found at %d\n", num, fnd+1);
  120                 } /* found */
  121         } /* for each query */
  122 } /* FUNCTION process */
  123 
  124 int main()
  125 {
  126     /* main */
  127     int moreToDo;
  128     int cse = 1;
  129 
  130     init();
  131     moreToDo = getInput();
  132     while (moreToDo)
  133         {
  134             /* while */
  135             printf("CASE# %d:\n", cse);
  136             cse++;
  137             process();
  138             moreToDo = getInput();
  139         } /* while */
  140 
  141     return EXIT_SUCCESS;
  142 } /* main */
  143