USACO Section 2.1 Hamming Codes
暴力
1 /* ID:linyvxi1
2 PROB:hamming
3 LANG:C++
4 */
5 #include <stdio.h>
6 #include <math.h>
7 #include <stdlib.h>
8 int N,B,D;
9 int max_search_num;
10 int final_result[130];
11 int num_found;
12
13 bool check(int next)
14 {
15 int s=0;
16 for(;s<num_found;s++){
17 int temp;
18 temp=next^final_result[s];
19 int count=0;
20 int i;
21 for(i=0;i<B;i++){
22 int out_num=temp&0x00000001;
23 if(out_num){
24 count++;
25 }
26 temp=temp>>1;
27 }
28 if(count<D){
29 return false;
30 }
31 }
32 return true;
33 }
34 int main()
35 {
36 freopen("hamming.in","r",stdin);
37 freopen("hamming.out","w",stdout);
38 scanf("%d%d%d",&N,&B,&D);
39 max_search_num=(int)pow(2,B)-1;
40 final_result[0]=0;
41 int i;
42 num_found=1;
43 for(i=1;i<=max_search_num;i++){
44 if(num_found==N){
45 break;
46 }
47 if(check(i)){//如果hamming距离符合要求
48 final_result[num_found++]=i;
49 }
50 }
51 int already_output=0;
52 for(i=0;i<num_found;i++){
53 printf("%d",final_result[i]);
54 already_output++;
55 if(already_output%10==0){
56 putchar('\n');
57 }else if(i!=num_found-1){
58 putchar(' ');
59 }else{
60 putchar('\n');
61 }
62 }
63 }