题目链接:UVa 729 - The Hamming Distance Problem
求下一个排列的水题,不过我还是学习到了一些东西。
a是char数组,可以使用memset(a,0,sizeof(a)) 清空a,这道题必须清空。以前经常使用这个函数给整型或者bool型的数组赋初值了,还没用过这个函数对char数组赋初值。
还有一个问题我特想知道,这个题怎么优化,怎么那么多大神提交的代码那么快.....
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX_N = 16 + 2;
int H,N,T;
char a[MAX_N];
int main()
{
cin>>T;
while(T--)
{
memset(a,0,sizeof(a));
cin>>N>>H;
int i,j;
for(i = 1;i <= H;i++)
a[N-i] = '1';
for(j = 0;j < N - H;j++)
a[j] = '0';
do
{
cout<<a<<endl;
}
while(next_permutation(a,a+N));
if(T)
cout<<endl;
}
return 0;
}