题目大意:
输入两个数H、N,H表示一个字符串的长度,该字符串由0,1组成,按字典序输出字符串包含N个1时候的各个组合。
#define Local
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAX 20
int H, N;
void print_binary (int n)
{
int i = 0;
int bin[MAX];
memset(bin, 0, sizeof(bin));
while(n)
{
bin[i++] = n % 2;
n /= 2;
}
for (i = H-1; i >= 0; i--)
cout << bin[i];
cout << endl;
}
int main()
{
#ifdef Local
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int t = 0, max = 0, i = 0, j = 0;
cin >> t;
while (t--)
{
cin >> H >> N;
max = pow(2, H)-1;
for (i = 0; i <= max; i++)
{
int temp = i, num = 0;
while (temp)
{
temp &= (temp-1);
num++;
}
if (N == num)
{
print_binary(i);
}
}
if (t)
cout << endl;
}
}