Computer Programming Contest Preparation

ToolBox - Source for: 127/12705/a.c



/home/toolbox/public_html/solutions/127/12705/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2015-10-01
   20  * Purpose: fun
   21  * Problem: 12705 - Breaking Board
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_LINE_LENGTH 105
   29 /* digits 48-57
   30    upper 65-90 */
   31 #define SPACE 32
   32 #define DIGIT_START 48
   33 #define DIGIT_STOP 58
   34 #define UPPER_START 65
   35 #define UPPER_STOP 91
   36 
   37 int numberOfTimes;
   38 int board[36] =
   39 {
   40     2,
   41     3,   3,
   42     4,   4,  4,
   43     5,   5,  5,  5,
   44     6,   6,  6,  6,  6,
   45     7,   7,  7,  7,  7,  7,
   46     8,   8,  8,  8,  8,
   47     9,   9,  9,  9,
   48     10, 10, 10,
   49     11, 11,
   50     12
   51 };
   52 int occurrences[37];
   53 char buff[MAX_LINE_LENGTH];
   54 
   55 void init()
   56 {
   57     /* FUNCTION init */
   58     scanf("%d ", &numberOfTimes);
   59 } /* FUNCTION init */
   60 
   61 void dump()
   62 {
   63     /* FUNCTION dump */
   64     int i;
   65 
   66     printf("Occurrences:\n");
   67     for (i=0; i<36; i++)
   68         {
   69             printf("%d ", occurrences[i]);
   70         }
   71     printf("\n");
   72 } /* FUNCTION dump */
   73 
   74 int compare(const void *a, const void *b)
   75 {
   76     /* FUNCTION compare */
   77     return ( *(int*)b - *(int*)a );
   78 } /* FUNCTION compare */
   79 
   80 void getInput()
   81 {
   82     /* FUNCTION getInput */
   83     fgets(buff, sizeof(buff), stdin);
   84     buff[strlen(buff) - 1] = '\0';
   85 } /* FUNCTION getInput */
   86 
   87 void process()
   88 {
   89     /* FUNCTION process */
   90     int i;
   91     int tot = 0;
   92 
   93     for (i=0; i<37; i++)
   94         {
   95             occurrences[i] = 0;
   96         }
   97     for (i=0; i<strlen(buff); i++)
   98         {
   99             /* for each character */
  100             if (isupper(buff[i]))
  101                 {
  102                     occurrences[buff[i] - 'A' + 10]++;
  103                 }
  104             else if (isdigit(buff[i]))
  105                 {
  106                     occurrences[buff[i] - '0']++;
  107                 }
  108         } /* for each character */
  109     qsort(occurrences, 36, sizeof(int), compare);
  110     for (i=0; 0<occurrences[i]; i++)
  111         {
  112             /* step through each  unique character found */
  113             tot = tot + occurrences[i] * board[i];
  114         } /* step through each  unique character found */
  115     printf("%d\n", tot);
  116 } /* FUNCTION process */
  117 
  118 int main()
  119 {
  120     /* main */
  121     int i;
  122 
  123     init();
  124     for (i=0; i<numberOfTimes; i++)
  125         {
  126             /* while */
  127             getInput();
  128             process();
  129         } /* while */
  130 
  131     return EXIT_SUCCESS;
  132 } /* main */
  133 
  134