Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/7/729/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: 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 int mask[17] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536};
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36     scanf("%d ", &numberOfTimes);
   37 } /* FUNCTION init */
   38 
   39 void dump()
   40 {
   41     /* FUNCTION dump */
   42 } /* FUNCTION dump */
   43 
   44 void getInput()
   45 {
   46     /* FUNCTION getInput */
   47     scanf(" %d %d ", &n, &h);
   48     DEBUG printf("(n = %d) (h = %d)\n", n, h);
   49 } /* FUNCTION getInput */
   50 
   51 int countBits(int x)
   52 {
   53     /* FUNCTION countBits */
   54     /* d - need value to add
   55      * a - number of bits left
   56      * b - number of 1s left
   57      */
   58     int i;
   59     int cnt = 0;
   60 
   61     while (x > 0)
   62         {
   63             /* while */
   64             cnt = cnt + (x & 1);
   65             x = x >> 1;
   66         } /* while */
   67     return cnt;
   68 } /* FUNCTION countBits */
   69 
   70 void printBin(int num, int bits)
   71 {
   72     /* FUNCTION printBin */
   73     int i;
   74 
   75     DEBUG printf("(bits = %d) (num = %d)\n", bits, num);
   76     for (i=bits-1; i>=0; i--)
   77         {
   78             /* for each bit */
   79             printf("%c", (num & mask[i]) ? '1' : '0');
   80         } /* for each bit */
   81     printf("\n");
   82 } /* FUNCTION printBin */
   83 
   84 void process()
   85 {
   86     /* FUNCTION process */
   87     int i;
   88     int upper;
   89 
   90     upper = mask[n];
   91     for (i=1; upper>i; i++)
   92         {
   93             /* loop through each possible value */
   94             /* printf("%d - %d\n", i, countBits(i)); */
   95             if (countBits(i) == h)
   96                 {
   97                     /* valid number */
   98                     printBin(i, n);
   99                 } /* valid number */
  100         } /* loop through each possible value */
  101 
  102 } /* FUNCTION process */
  103 
  104 int main()
  105 {
  106     /* main */
  107     int i;
  108 
  109     init();
  110     for (i=0; i<numberOfTimes; i++)
  111         {
  112             /* while */
  113             getInput();
  114             if (0 != i)
  115                 {
  116                     printf("\n");
  117                 }
  118             process();
  119         } /* while */
  120 
  121     return EXIT_SUCCESS;
  122 } /* main */
  123 
  124