Computer Programming Contest Preparation

ToolBox - Source for: 7/729/b.c



/home/toolbox/public_html/solutions/7/729/b.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: 2019-09-03
   20  * Purpose: fun
   21  * Problem: 729
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 int numberOfTimes;
   29 int n;
   30 int h;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35     scanf("%d ", &numberOfTimes);
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 void getInput()
   44 {
   45     /* FUNCTION getInput */
   46     scanf(" %d %d ", &n, &h);
   47 } /* FUNCTION getInput */
   48 
   49 void doit(int d, int a, int b)
   50 {
   51     /* FUNCTION doit */
   52     /* d - need value to add
   53      * a - number of bits left
   54      * b - number of 1s left
   55      */
   56     int i;
   57 
   58     DEBUG printf("\ndebug: (d,%d) (a,%d) b,%d)\n", d, a, b);
   59     printf("%d", d);
   60     if (0 == b)
   61         {
   62             printf("\n");
   63         }
   64     else
   65         {
   66             /* some bits left */
   67             if (a == b)
   68                 {
   69                     /* all must be 1s */
   70                     for (i=0; i<a; i++)
   71                         {
   72                             printf("1");
   73                         }
   74                     printf("\n");
   75                 } /* all must be 1s */
   76             else
   77                 {
   78                     /* not all 1s left */
   79                     if (0 == b)
   80                         {
   81                             /* all 0s left */
   82                             for (i=0; i<b; i++)
   83                                 {
   84                                     printf("0");
   85                                 }
   86                             printf("\n");
   87                         } /* all 0s left */
   88                     else
   89                         {
   90                             /* some combo of 0s and 1 left */
   91                             doit(0, a-1, b);
   92                             doit(1, a-1, b-1);
   93                         } /* some combo of 0s and 1 left */
   94                 } /* not all 1s left */
   95         } /* some bits left */
   96 } /* FUNCTION doit */
   97 
   98 void process()
   99 {
  100     /* FUNCTION process */
  101     int a;
  102     int b;
  103 
  104     a = n - 1;
  105     b = h;
  106     DEBUG printf("%d %d\n", a, b);
  107     if (n > h)
  108         {
  109             doit(0, a, b);
  110         }
  111     doit(1, a, b-1);
  112     printf("\n");
  113 
  114 } /* FUNCTION process */
  115 
  116 int main()
  117 {
  118     /* main */
  119     int i;
  120 
  121     init();
  122     for (i=0; i<numberOfTimes; i++)
  123         {
  124             /* while */
  125             getInput();
  126             process();
  127         } /* while */
  128 
  129     return EXIT_SUCCESS;
  130 } /* main */
  131 
  132