Computer Programming Contest Preparation

ToolBox - Source for: 9/948/a.c



/home/toolbox/public_html/solutions/9/948/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2021-12-15
   21  * Purpose: fun
   22  * Problem: 948 - Fibonaccimal Base
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 /* greedy approach */
   30 
   31 #define MAX_FIB 46
   32 #define MAX_LINE 50
   33 
   34 int fib[MAX_FIB];
   35 int fibCnt;
   36 int numberOfTimes;
   37 int num;
   38 char line[MAX_LINE];
   39 int cnt;
   40 
   41 void init()
   42 {
   43     /* FUNCTION init */
   44     int i;
   45 
   46     scanf("%d ", &numberOfTimes);
   47     fib[0] = 0;
   48     fib[1] = 1;
   49     for (i=0; (MAX_FIB-2)>i; i++)
   50         {
   51             /* for */
   52             fib[i+2] = fib[i] + fib[i+1];
   53         } /* for */
   54     fibCnt = MAX_FIB -1;
   55     DEBUG printf("fib[%d] = %d\n", fibCnt, fib[fibCnt]);
   56 } /* FUNCTION init */
   57 
   58 void dump()
   59 {
   60     /* FUNCTION dump */
   61 } /* FUNCTION dump */
   62 
   63 void getInput()
   64 {
   65     /* FUNCTION getInput */
   66     scanf(" %d ", &num);
   67 } /* FUNCTION getInput */
   68 
   69 void process()
   70 {
   71     /* FUNCTION process */
   72     int i;
   73     int digitFound = FALSE;
   74 
   75     cnt = 0;
   76     printf("%d = ", num);
   77     for (i=fibCnt-1; (1 < i); i--)
   78         {
   79             /* for */
   80             line[cnt] = 0;
   81             DEBUG printf("(num %d) (fib[%d] = %d) (line%d [%s])\n", num, i, fib[i], cnt, line);
   82             if (fib[i] <= num)
   83                 {
   84                     /* found a number to use */
   85                     digitFound = TRUE;
   86                     line[cnt] = '1';
   87                     num = num - fib[i];
   88                     cnt++;
   89                     i--; /* skip next fib number sense we had a match */
   90                     if (1 < i)
   91                         {
   92                             /* 1 is always followed by 0 unless at first fib number */
   93                             line[cnt] = '0';
   94                             cnt++;
   95                         } /* 1 is always followed by 0 unless at first fib number */
   96                 } /* found a number to use */
   97             else if (digitFound)
   98                 {
   99                     /* zeroes count now */
  100                     line[cnt] = '0';
  101                     cnt++;
  102                 } /* zeroes count now */
  103         } /* for */
  104     line[cnt] = 0;
  105     printf("%s (fib)\n", line);
  106 } /* FUNCTION process */
  107 
  108 int main()
  109 {
  110     /* main */
  111     int i;
  112 
  113     init();
  114     for (i=0; i<numberOfTimes; i++)
  115         {
  116             /* while */
  117             getInput();
  118             process();
  119         } /* while */
  120 
  121     return EXIT_SUCCESS;
  122 } /* main */
  123 
  124