Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/104/10491/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 #define MAX_LINE 257
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2022-10-26
   21  * Purpose: fun
   22  * Problem: 10491 - Cowes and Cars
   23  */
   24 
   25 /*
   26  * This template reads lines of data at a time until end of file.
   27  */
   28 
   29 int ncows;
   30 int ncars;
   31 int nshows;
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 int getInput()
   44 {
   45     /* FUNCTION getInput */
   46     int dataReadFlag;
   47 
   48     dataReadFlag = (3 == scanf(" %d %d %d ", &ncows, &ncars, &nshows));
   49     return (dataReadFlag);
   50 } /* FUNCTION getInput */
   51 
   52 void process()
   53 {
   54     /* FUNCTION process */
   55     /* percent = (cow choice percent + car choice percent) / (ncars + ncows)
   56      * cow choice percent:
   57      * (ncows * ncars) / (ncars + ncows - nshows - 1)   (remove the cow we chose to start with
   58      * car choice percent:
   59      * (ncars * (ncars - 1)) / (ncars - 1 + ncows - nshows)
   60      */
   61     double cars;
   62     double cows;
   63     double shows;
   64     double p1;
   65     double p2;
   66     double answer;
   67 
   68     cars = ncars;
   69     cows = ncows;
   70     shows = nshows;
   71 
   72     p1 = (cows * cars) / (cars + cows - shows - 1);
   73     p2 = (cars * (cars - 1)) / (cars - 1 + cows - shows);
   74     answer = (p1 + p2) / (cars + cows);
   75     printf("%.5lf\n", answer);
   76 } /* FUNCTION process */
   77 
   78 int main()
   79 {
   80     /* main */
   81     int moreToDo;
   82 
   83     init();
   84     moreToDo = getInput();
   85     while (moreToDo)
   86         {
   87             /* while */
   88             process();
   89             moreToDo = getInput();
   90         } /* while */
   91 
   92     return EXIT_SUCCESS;
   93 } /* main */
   94