Hamming Codes(2진수 매거진)
3349 단어 code
Rob Kolstad
Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of length B bits (1 <= B <= 8), such that each of the codewords is at least Hamming distance of D (1 <= D <= 7) away from each of the other codewords. The Hamming distance between a pair of codewords is the number of binary bits that differ in their binary notation. Consider the two codewords 0x554 and 0x234 and their differences (0x554 means the hexadecimal number with hex digits 5, 5, and 4):
0x554 = 0101 0101 0100
0x234 = 0010 0011 0100
Bit differences: xxx xx
Since five bits were different, the Hamming distance is 5.
PROGRAM NAME: hamming
INPUT FORMAT
N, B, D on a single line
SAMPLE INPUT (file hamming.in)
16 7 3
OUTPUT FORMAT
N codewords, sorted, in decimal, ten per line. In the case of multiple solutions, your program should output the solution which, if interpreted as a base 2^B integer, would have the least value.
SAMPLE OUTPUT (file hamming.out)
0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127
제목:
N (1 ~ 64), B (1 ~ 8), D (1 ~ 7)를 제시하고 N 개의 B 비트 바이너리 수를 출력합니다. 이 수를 임의로 바이너리로 바꾸면 다른 수는 적어도 D 개가 있어야 합니다. 이 서열은 와 가장 작은 서열입니다.
생각:
일일이 열거하다.최대 8비트 2진법, 설명 최대 255, 과감한 2진법 매거, 1A.
AC:
/*
TASK:hamming
LANG:C++
ID:sum-g1
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
int n,b,d,ans;
int bin_a[10],bin_b[10];
int fin[70];
int test(int num)
{
int k = 0;
while(num)
{
k++;
bin_a[k] = num % 2;
num /= 2;
}
for(int i = 1;i <= ans;i++)
{
int change = fin[i];
k = 0;
memset(bin_b,0,sizeof(bin_b));
while(change)
{
k++;
bin_b[k] = change % 2;
change /= 2;
}
int sum = 0,temp = 0;
for(k = 1;k <= b;k++)
{
if(bin_a[k] != bin_b[k]) sum++;
if(sum >= d)
{
temp = 1;
break;
}
}
if(!temp) return 0;
}
return 1;
}
int main()
{
freopen("hamming.in","r",stdin);
freopen("hamming.out","w",stdout);
ans = 1;
scanf("%d%d%d",&n,&b,&d);
fin[1] = 0;
printf("%d ",fin[1]);
for(int i = 1;i <= pow(2,b) - 1;i++)
{
memset(bin_a,0,sizeof(bin_a));
if(test(i))
{
ans++;
fin[ans] = i;
printf("%d",i);
if(!(ans % 10) || ans == n) printf("
");
else printf(" ");
}
if(ans == n) break;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Visual Studio 코드에 무료 Cursive(Italic) 글꼴 추가우리 모두는 기능과 확장성 때문에 Visual Studio 코드 편집기를 좋아합니다. 이 기사에서는 필기체(이탤릭체) 글꼴을 완전 무료로 설정하는 방법에 대한 유용한 팁을 보여드리겠습니다. 그 때문에 사람들은 Ope...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.