The Hamming Distance Problem |
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.
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 .
Print a blank line between datasets.
1 4 2
0011 0101 0110 1001 1010 1100
解析:给出两个数,一个是总位数,另一个是1的位数,求全排列
#include <iostream> #include <algorithm> #include <string.h> using namespace std; int main() { int t; int n,h; while(cin >> t) { while( t-- ) { cin >> n >> h; int a[30]; memset(a,0,sizeof(a)); for(int i = 0; i < n-h; i++) { a[i] = 0; } for(int i = n - h; i < n; i++) { a[i] = 1; } do { for(int i = 0; i < n; i++) { cout << a[i]; } cout << endl; } while( next_permutation(a,a+n)); if(t) { cout<<endl; } } } return 0; }