Computer Programming Contest Preparation

ToolBox - Source for: naq/naq2022/k.c



/home/toolbox/public_html/solutions/naq/naq2022/k.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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date:
   19  * Purpose: fun
   20  * Problem: NAQ - k
   21  */
   22 
   23 /*
   24  * This template reads lines of data at a time until end of file.
   25  */
   26 
   27 #define add 0
   28 #define sub 1
   29 #define mul 2
   30 #define div 3
   31 
   32 int a;
   33 int b;
   34 int c;
   35 
   36 void getInput()
   37 {
   38     /* FUNCTION getInput */
   39     scanf(" %d %d %d ", &a, &b, &c);
   40 } /* FUNCTION getInput */
   41 
   42 int tryIt(int x, int y, int op)
   43 {
   44     /* FUNCTION tryIt */
   45     int ret;
   46 
   47     if (div == op)
   48         {
   49             /* deal with division issues */
   50             if (0 == y)
   51                 {
   52                     /* pretend multiplication */
   53                     ret = x * y;
   54                 } /* pretend multiplication */
   55             else if (0 != (x % y))
   56                 {
   57                     /* division not allowed */
   58                     ret = x * y;
   59                 } /* division not allowed */
   60             else
   61                 {
   62                     /* normal div case */
   63                     ret = x / y;
   64                 } /* normal div case */
   65         } /* deal with division issues */
   66     else
   67         {
   68             /* do operation */
   69             switch (op)
   70                 {
   71                 /* switch  */
   72                 case add:
   73                     ret = x + y;
   74                     break;
   75                 case sub:
   76                     ret = x - y;
   77                     break;
   78                 case mul:
   79                     ret = x * y;
   80                     break;
   81                 case div:
   82                     ret = x / y;
   83                     break;
   84                 } /* switch  */
   85         } /* do operation */
   86     return ret;
   87 } /* FUNCTION tryIt */
   88 
   89 void process()
   90 {
   91     /* FUNCTION process */
   92     int best = 1000001;
   93     int t1;
   94     int t2;
   95 
   96     if ((0 == a) || (0 == b) || (0 == c))
   97         {
   98             /* trivial case - any of them 0, just multiply */
   99             printf("0\n");
  100         } /* trivial case - any of them 0, just multiply */
  101     else if (a == b)
  102         {
  103             /* trivial case */
  104             printf("0\n");
  105         } /* trivial case */
  106     else
  107         {
  108             /* other cases */
  109             t1 = tryIt(a, b, add);
  110             t2 = tryIt(t1, c, add);
  111             if ((t2 >= 0) && (best > t2))
  112                 {
  113                     best = t2;
  114                 }
  115             t2 = tryIt(t1, c, sub);
  116             if ((t2 >= 0) && (best > t2))
  117                 {
  118                     best = t2;
  119                 }
  120             t2 = tryIt(t1, c, mul);
  121             if ((t2 >= 0) && (best > t2))
  122                 {
  123                     best = t2;
  124                 }
  125             t2 = tryIt(t1, c, div);
  126             if ((t2 >= 0) && (best > t2))
  127                 {
  128                     best = t2;
  129                 }
  130 
  131             t1 = tryIt(a, b, sub);
  132             t2 = tryIt(t1, c, add);
  133             if ((t2 >= 0) && (best > t2))
  134                 {
  135                     best = t2;
  136                 }
  137             t2 = tryIt(t1, c, sub);
  138             if ((t2 >= 0) && (best > t2))
  139                 {
  140                     best = t2;
  141                 }
  142             t2 = tryIt(t1, c, mul);
  143             if ((t2 >= 0) && (best > t2))
  144                 {
  145                     best = t2;
  146                 }
  147             t2 = tryIt(t1, c, div);
  148             if ((t2 >= 0) && (best > t2))
  149                 {
  150                     best = t2;
  151                 }
  152             DEBUG printf("%d %d %d %d %d %d\n", a, b, c, t1, t2, best);
  153 
  154             t1 = tryIt(a, b, mul);
  155             t2 = tryIt(t1, c, add);
  156             if ((t2 >= 0) && (best > t2))
  157                 {
  158                     best = t2;
  159                 }
  160             t2 = tryIt(t1, c, sub);
  161             if ((t2 >= 0) && (best > t2))
  162                 {
  163                     best = t2;
  164                 }
  165             t2 = tryIt(t1, c, mul);
  166             if ((t2 >= 0) && (best > t2))
  167                 {
  168                     best = t2;
  169                 }
  170             t2 = tryIt(t1, c, div);
  171             if ((t2 >= 0) && (best > t2))
  172                 {
  173                     best = t2;
  174                 }
  175 
  176             t1 = tryIt(a, b, div);
  177             t2 = tryIt(t1, c, add);
  178             if ((t2 >= 0) && (best > t2))
  179                 {
  180                     best = t2;
  181                 }
  182             t2 = tryIt(t1, c, sub);
  183             if ((t2 >= 0) && (best > t2))
  184                 {
  185                     best = t2;
  186                 }
  187             t2 = tryIt(t1, c, mul);
  188             if ((t2 >= 0) && (best > t2))
  189                 {
  190                     best = t2;
  191                 }
  192             t2 = tryIt(t1, c, div);
  193             if ((t2 >= 0) && (best > t2))
  194                 {
  195                     best = t2;
  196                 }
  197             printf("%d\n", best);
  198         } /* other cases */
  199 } /* FUNCTION process */
  200 
  201 int main()
  202 {
  203     /* main */
  204 
  205     getInput();
  206     process();
  207 
  208     return EXIT_SUCCESS;
  209 } /* main */
  210