forked from chitwang/iitk-sem1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits-bits.c
73 lines (59 loc) · 1.84 KB
/
bits-bits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*In the input, you will be given a strictly positive integer K denoting the length of the strings you have to print. In your output, on each line, you have to print a string of length K using only the characters '0' and '1' (without quotes). The strings must be printed in lexicographically increasing order i.e. if you think of these strings as numbers, the numbers should appear in increasing order.
The only property these strings must satisfy is that no two consecutive characters in the strings you generate can be the character '0'. Two or more consecutive characters can be '1' but two consecutive characters cannot be '0'.
Note
The first character in the string can freely be 0 or 1 since there is no previous character to cause consecutive 0. However, second character onwards, we must have a 0 only if the previous character was not 0 to avoid consecutive 0.
Be sure to print all leading 0 in the output. Every string you print must contain k characters.
There should be no extra spaces anywhere. There MUST be a trailing newline after the last string printed.
Example Input
2
Example Output
01
10
11
Explanation
00 is an illegal string.*/
// solution:
#include <stdio.h>
#include <math.h>
void func(int n, int k)
{
int t = n;
int bin[32];
int i = 0;
while (i < k)
{
bin[i] = t % 2;
t /= 2;
i++;
}
int flag = 0;
for (int j = 0; j < i - 1; j++)
{
if (bin[j] == 0 && bin[j + 1] == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
for (int j = i - 1; j >= 0; j--)
{
printf("%d", bin[j]);
if (j == 0)
{
printf("\n");
}
}
}
}
int main()
{
int k;
scanf("%d", &k);
for (int i = 1; i < (pow(2, k)); i++)
{
func(i, k);
}
return 0;
}