Description
The Hamming Distance Problem
Thinking
枚举排列问题,从左到右递归处理。
Answer
#include <iostream>
using namespace std;
int ds;
int N, H;
int count(int* A, int num, int cur){
int count = 0;
for (int i = 0; i < cur; ++i) {
if (A[i] == num)
count++;
}
return count;
}
void print_permutation(int* A, int cur){
if (cur == N){
for (int i = 0; i < N; ++i)
cout << A[i];
cout << endl;
}
else{
for (int bit = 0; bit <= 1; ++bit) {
// 若在前面出现的次数符合要求
int ok = (count(A, bit, cur) < (bit == 1 ? H : N-H) ? 1 : 0);
if (ok){
A[cur] = bit;
print_permutation(A, cur+1);
}
}
}
}
int main(){
cin >> ds;
while(ds--){
cin >> N >> H;
int A[N] = {0};
print_permutation(A, 0);
if (ds) cout << endl;
}
}