joj1466

 1466: The Hamming Distance Problem

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 417 202 Standard

The Hamming distance between two strings of bits (binary integers) is the number of corresponding bit positions that differ. This can be found by using XOR on corresponding bits or equivalently, by adding corresponding bits (base 2) without a carry. For example, in the two bit strings that follow:

                               A      0 1 0 0 1 0 1 0 0 0
                               B      1 1 0 1 0 1 0 1 0 0
                            A XOR B = 1 0 0 1 1 1 1 1 0 0

The Hamming distance (H) between these 10-bit strings is 6, the number of 1's in the XOR string.

Input

N, the length of the bit strings and  H, the Hamming distance.

Output

A list of all possible bit strings of length  N that are Hamming distance  H from the bit string containing all 0's (origin). That is, all bit strings of length  N with exactly  H  1's printed in ascending lexicographical order.


The number of such bit strings is equal to the combinatorial symbol C(N,H). This is the number of possible combinations of N-H zeros and H ones. It is equal to 


This number can be very large. The program should work for .

Sample Input

4 2

Sample Output

0011
0101
0110
1001
1010
1100





这个题的难点在于将上述的输出结果的排列顺序找出来。

#include<stdio.h>
#include<string.h>
int n,m;
int a[20];
void find(int u,int v)
{
    if(u==n+1)
    {
        if(v==0)
        {
            for(int i=1;i<=n;i++)
            printf("%d",a[i]);
            printf("\n");
        }
        return ;
    }
    a[u]=0;
    find(u+1,v);
    a[u]=1;
    find(u+1,v-1);
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(a,0,sizeof(a));
        find(1,m);
    }
    return 0;
}

你可能感兴趣的:(joj1466)