首先0在num[]中,然后枚舉和已經加入num[]的數比較,如果符合,就加進集合中。
/*
ID:Ryan
PROG:hamming
LANG:C++
*/
#include
<iostream>
#include
<fstream>
using namespace std;
int num[65];
int n,b,d;
int cnt=1;
void check(int t)
{   
int i,j;
    
int k;
    
bool can=1;
    
for(i=1;i<=cnt;i++)//對每一個加進集合的數進行比較 
    {   k=0;
        
for(j=0;j<b;j++)//如果距離<d,排除 
        { 
            
if(t&(1<<j)^(num[i]&(1<<j)))
                k
++
        }
        
if(k<d)
        {   can
=0;
            
break;
        } 
    }
    
if(can==1)//如果符合情況,加進去 
        num[++cnt]=t;
    
return ;
}
int main()
{   ifstream fin(
"hamming.in");
    ofstream fout(
"hamming.out");
    
int q,j;
    fin
>>n>>b>>d;
    num[cnt]
=0;
    
for(q=1;q<(1<<b);q++)
    {
        check(q);
        
if(cnt==n)
            
break;
    }
    
for(j=1;j<cnt;j++)
    {   
if(j%10==0)
            fout
<<num[j]<<endl;
        
else  fout<<num[j]<<" ";
    }
    fout
<<num[j]<<endl;
    
//system("pause");
    return 0;
}