USACO 2.1 Hamming Codes(位运算枚举)
各种位运算,题意好纠结啊,在翻译帮助下,居然1Y了。。。
1 /* 2 ID: cuizhe 3 LANG: C++ 4 TASK: hamming 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 int que[100]; 12 int main() 13 { 14 int n,b,d,i,j,k,top,t,num; 15 freopen("hamming.in","r",stdin); 16 freopen("hamming.out","w",stdout); 17 scanf("%d%d%d",&n,&b,&d); 18 que[1] = 0; 19 top = 1; 20 for(i = 1;i < (1<<b);i ++) 21 { 22 for(j = 1;j <= top;j ++) 23 { 24 t = que[j]^i; 25 num = 0; 26 for(k = 0;k < b;k ++) 27 { 28 if((1<<k)&t) 29 num++; 30 } 31 if(num < d) 32 break; 33 } 34 if(j == top+1) 35 { 36 que[++top] = i; 37 } 38 if(top == n) break; 39 } 40 for(i = 1;i <= top;i ++) 41 { 42 if(i%10 == 0) 43 printf("%d\n",que[i]); 44 else if(i == top) 45 printf("%d\n",que[i]); 46 else 47 printf("%d ",que[i]); 48 } 49 return 0; 50 }