利用C++中的next_permutation()去列舉所有排列即可。
C++(0.000)
/*******************************************************/
/* UVa 729 The Hamming Distance Problem */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2016/07/27 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int main(){
int numberOfDataset;
while( scanf("%d", &numberOfDataset) != EOF ){
for( int testNumber = 0 ; testNumber < numberOfDataset ; ++testNumber ){
if( testNumber > 0 ){
printf("\n");
}
int N, H;
scanf("%d%d", &N, &H);
string s;
for( int i = 0 ; i < N-H ; ++i ){
s += '0';
}
for( int i = 0 ; i < H ; ++i ){
s += '1';
}
do{
printf("%s\n", s.c_str());
} while( next_permutation(s.begin(), s.end()) );
}
}
return 0;
}