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