因为要求最终序列化为2^B进制数后最小,所以最小从0开始一一枚举&检验即可。
/* ID:chenjiong PROG:hamming LANG:C++ */ #include <stdio.h> #include <string.h> int N,B,D; int ans[64][9]; int num; int limit[9] = {0,1,3,7,15,31,63,127,255}; int cnt; void binary(int x,int* tmp) { tmp[cnt++] = x % 2; if ( x > 0 ) binary( x / 2,tmp); } int hamming_distance(int i,int j) { int d = 0; int k; for ( k = 0; k < 8; k++) if ( ans[i][k] != ans[j][k] ) d++; return d; } int is_accepted(int cur) { int i; for ( i = cur - 1; i >= 0; i--) if ( hamming_distance(i,cur) < D ) return false; return true; } int main() { freopen("hamming.in","r",stdin); freopen("hamming.out","w",stdout); scanf("%d%d%d",&N,&B,&D); num = 1; int x; for ( x = 1; x <= limit[B]; x++) { cnt = 0; binary(x,ans[num]); ans[num][8] = x; if ( is_accepted(num) ) num++; if ( num == N ) break; } int t = 0; int i; for ( i = 0; i < N - 1; i++) { printf("%d ",ans[i][8]); t++; if ( t == 9 && i < N - 2 ) { printf("%d\n",ans[++i][8]); t = 0; } } printf("%d\n",ans[N - 1][8]); return 0; }