首页 > 代码库 > 729 - The Hamming Distance Problem

729 - The Hamming Distance Problem

 

// 题意:
// 输入两个整数N, H,按照字典序输出所有长度为N,恰好包含H个1的01串
// 规模:1<=H<=N<=16
// 算法A:2^N枚举,输出1的个数为H的。采用递归枚举

// 从bits[d]开始确定,已经用了c0个0和c1个1

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int n,h;
int buf[16];
void solve(int c0, int c1, int d)
{
    if(d==n)
    {
        if(c1==h)
        {
            for(int i=0;i<n;i++)
                printf("%d", buf[i]);
            printf("\n");
        }
        return;
    }

    if(c0<n-h)
    {
        buf[d]=0;
        solve(c0+1, c1, d+1);
    }

    if(c1<h)
    {
        buf[d]=1;
        solve(c0, c1+1, d+1);
    }

}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d %d", &n, &h);
        solve(0, 0, 0);
        if(T)
            printf("\n");
    }

    return 0;
}