## Problem C: Pattern Generator

Write a program that repeatedly reads two numbers n and k and prints all bit patterns of length n with k ones in descending order (when the bit patterns are considered as binary numbers). You may assume that 30 ≥ n > 0, 8 > k ≥ 0, and nk. The first number in the input gives the number of pairs n and k. The numbers n and k are separated by a single space. Leading zeroes in a bit pattern should be included. See the example below.

```3
2 1
2 0
4 2```

### Sample Output

```The bit patterns are
10
01

The bit patterns are
00

The bit patterns are
1100
1010
1001
0110
0101
0011
```

Point Value: 10
Time Limit: 10.00s
Memory Limit: 16M

Problem Types: [Show]

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

• (0/0)
Can somebody help me check my code?
My samples are right but I just cannot get any point.Thanks a lot !

• (0/0)
Which of your own test cases have you tried?

• (0/0)
Do you need to use recursion for this problem?
Only asking because it says in problem type "recursion" and a comment says you need to use recursion

• (1/0)
You can use anything you want (other than hardcoding); the problem type is just a very general umbrella.

• (0/0)
Can someone check my code? I'm pretty sure the algorithm works. Thanks.

• (0/0)
You go out of bounds.

• (0/0)
Can you be A little bit more detail? Is it array out of bound or int out of bound?

• (0/0)
Generally, out of bounds implies array. If I meant you exceeded the limits of a numeric type, I'd say that you overflowed (on ints or doubles) or underflowed (on doubles).

Edit: However, after looking a bit more, it's not your only problem. Try some more test cases.

• (0/0)
Thanks. My code works.

• (0/0)
I'm pretty sure I'm doing this problem right, can someone help me?

• (0/0)
":"
lol

also your algorithm does not print the patterns in the order specifieddsdss

"Write a program that prints all bit patterns of length n with k ones in descending order "

• (0/0)
Still doesn't work. Does that mean i did this problem right >_<

• (0/0)
What's wrong with my program? I tried it with the test cases and I got them right, but I got wrong answer for it.

~helios26

• (1/0)
Try some more test cases. Trying all (n,k) with 0 <= k <= n <= 5 might be a good starting point.

• (0/0)
I'm still getting 0/10 and I tried all of the n, k under 5 and I also checked some bigger nums. I found my error with clearing one of the strings, but nothing else. Any hints?

Thanks,
~helios26

• (0/0)
I'm afraid your algorithm just isn't correct (though it does work on some cases, such as small ones). You need to use a concept known as "recursion".

• (0/0)
do we just output nothing if no patterns fit the given conditions (n and k)?

• (0/0)
How is that possible?

• (0/1)
when k>n

• (5/0)
"You may assume that 30 >= n > 0, 8 > k >= 0, and n >= k"

Maybe read the problem statement more carefully next time.