The description
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 addingcorresponding bits (base 2) without a carry. For example, in the two bitstrings that follow:
A0 1 0 0 1 0 1 0 0 0
B1 1 0 1 0 1 0 1 0 0
AXOR B = 1 0 0 1 1 1 1 1 0 0
The Hamming distance (H) between these10-bit strings is 6, the number of 1's in the XOR string.
Input
Input consists of several datasets. The firstline of the input contains the number of datasets, and it's followed by a blankline. Each dataset contains N, the length of the bit strings and H,the Hamming distance, on the same line. There is a blank line between testcases.
Output
For each dataset print a list of all possible bitstrings of length N that are Hamming distance Hfromthe bit string containing all 0's (origin). That is, all bit strings of length N withexactlyH 1'sprinted 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 zerosand H ones. It is equal to
This number can be very large. The program shouldwork for .
Print a blank line between datasets.
Sample input
1
4 2
Sample output
0011
0101
0110
1001
1010
1100
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
char a[25];
int n,lenth,m,i;
while(~scanf("%d",&n))
{
while(n--)
{
scanf("%d%d",&lenth,&m);
memset(a,0,sizeof(a));//memset中对数组清零,该处的0相当于'\0',而不是'0'
for(i=0;i<lenth-m;i++)
a[i]='0';
for(;i<lenth;i++)
a[i]='1';
printf("%s\n",a);
while(next_permutation(a,a+strlen(a)))
{
printf("%s\n",a);
}
if(n)
printf("\n");
}
}
return 0;
}