USACO-Section2.1 Hamming Codes【暴力枚举】
2017-07-12 11:29
381 查看
题目描述:
给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位的“Hamming距离”(1 <= D <= 7)。“Hamming距离”是指对于两个编码,他们二进制表示法中的不同二进制位的数目。看下面的两个编码 0x554 和 0x234(0x554和0x234分别表示两个十六进制数):0x554 = 0101 0101 0100
0x234 = 0010 0011 0100
不同位为 ..xxx. ..xx
因为有五个位不同,所以“Hamming距离”是 5。
(翻译来源:NOCOW)
INPUT FORMAT:
(file hamming.in)一行,包括 N, B, D。
OUTPUT FORMAT:
(file hamming.out)N 个编码(用十进制表示),要排序,十个一行。如果有多解,你的程序要输出这样的解:假如把它化为2^B进制数,它的值要最小。
SAMPLE INPUT
16 7 3
SAMPLE OUTPUT
0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127
解题思路:
由于这道题数据量比较小,可以直接暴力枚举,判断与之前保存的数字是否满足海明数关系即可。#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> int N,B,D,a[65],acount=1; int judge(int a[],int b){//判断是否符合要求 int i,temp,tcount; for(i=0;i<acount;i++){ tcount=0; temp=a[i]^b; while(temp){ tcount++; temp=temp&(temp-1); } if(tcount<D)return 0; } return 1; } int main(){ FILE *fin = fopen ("hamming.in", "r"); FILE *fout = fopen ("hamming.out", "w"); fscanf(fin,"%d%d%d",&N,&B,&D); a[0]=0; int i; for(i=1;i<1<<B;i++){ if(judge(a,i)) { a[acount]=i; acount++; } if(acount>=N) break; } for(i=0;i<N;i++){ if(i%10==9||i==N-1) fprintf(fout,"%d\n",a[i]); else fprintf(fout,"%d ",a[i]); } exit(0); }
相关文章推荐
- USACO-Section1.5 Prime Palindromes【暴力枚举】
- USACO-Section 1.2 Palindromic Squares【暴力枚举】
- USACO-Section1.4 Arithmetic Progressions【暴力枚举】
- USACO-Section 2.1 Hamming Codes (DFS)
- USACO-Section3.1 Contact【暴力枚举】
- USACO-Section1.2 Dual Palindromes【暴力枚举】【查找】
- USACO-Section2.1 Healthy Holsteins [搜索][枚举]
- USACO-Section2.1 Hamming Codes(深度优先搜索)
- USACO-Section1.3 Prime Cryptarithm【暴力枚举】
- USACO Section 2.1 Ordered Fractions(枚举)
- USACO-Section1.3 Combination Lock【暴力枚举】
- USACO-Section1.3 Wormholes 【深度优先搜索】【暴力枚举】
- USACO Section2.1 Hamming Codes 解题报告 【icedream61】
- USACO Section 2.1 Hamming Codes (dfs)
- USACO-Section1.4 Arithmetic Progressions[其他][暴力枚举]
- USACO 2.1 Hamming Codes(位运算枚举)
- USACO-Section2.2 Runaround Numbers【暴力枚举】
- USACO-Section1.2 Name That Number【暴力枚举】【二分查找】
- USACO-Section1.3 Ski Course Design【暴力枚举】
- USACO Section 3.4 Closed Fences - 暴力枚举..