USACO SECTION 2.1 Hamming Codes

本文介绍了一种基于Hamming距离的编码算法实现方法,该算法能够生成满足特定长度及距离要求的码字集合。通过使用C语言编程,文章提供了一个具体的实现方案,包括输入输出格式及示例。
Hamming Codes
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

 

#include<stdlib.h>
#include<string.h>

int CodeWords[64];
int WordsNumber,Length,Distance;

int IsTrue(int i,int NextWords)
{
	int j,k;
	int a,b,count;
	for(j=0;j<i;j++)
	{
		a=CodeWords[j];
		b=NextWords;
		count=0;
		for(k=0;k<Length;k++)
		{
			if((a&1)!=(b&1))
				count++;
			a>>=1;
			b>>=1;
		}
		if(count<Distance)
			return 0;
	}
	return 1;
}

int main()
{
	FILE *fin,*fout;
	fin=fopen("hamming.in","r");
	fout=fopen("hamming.out","w");
	
	fscanf(fin,"%d %d %d",&WordsNumber,&Length,&Distance);
	
	int i,j,k;
	int NextWord;
	
	CodeWords[0]=0;
	for(i=1;i<WordsNumber;i++)
	{
		NextWord=CodeWords[i-1]+1;
		while(!IsTrue(i,NextWord))
		{
			NextWord++;
		}	
		CodeWords[i]=NextWord;
	}
	
	for(i=0;i<WordsNumber;i++)
	{
		fprintf(fout,"%d",CodeWords[i]);
		if((i+1)%10==0)
			fprintf(fout,"\n");
		else
		if(i!=WordsNumber-1)
			fprintf(fout," ");
	}
	if(i%10!=0)
		fprintf(fout,"\n");
	
	return 0;
}


 

感觉题好水。。没有以前花好长时间做出来的那种喜悦的感觉了= =|||

多去NOCOW上面看看别人的做法。看看能不能多点收获

在P1458 USACO 2.1 有序分数 Ordered Fractions 的题目中,你需要处理一组分数,每个分数由两个整数表示,即分子m和分母d,然后对这些分数按照它们的小数形式进行非递减排序。以下是解决这个问题的一般步骤: 1. **创建结构体**: 定义一个包含分子和分母的结构体或类,例如在C++中可以是`struct Fraction { int numerator; int denominator; }`。 2. **转换为小数**: 对于每个分数,需要将其转换为浮点数以便进行比较。这可以通过除法操作 `float fraction = (float)m / d;` 来完成。 3. **比较函数**: 编写一个用于比较两个分数大小的函数,通常通过比较它们的小数形式。如果分数a小于b,则返回负数;等于则返回0;大于则返回正数。 ```cpp bool compareFractions(const Fraction &a, const Fraction &b) { return a.numerator * b.denominator > b.numerator * a.denominator; } ``` 4. **排序算法**: 使用适当的排序算法,如`std::sort`(对于C++),对分数数组进行排序。这里,你可以传递自定义的比较函数给`sort`,它会根据这个函数的规则对元素进行排序。 ```cpp std::vector<Fraction> fractions; // ... 添加分数到fractions列表中 std::sort(fractions.begin(), fractions.end(), compareFractions); ``` 5. **存储结果**: 排序完成后,数组`fractions`就包含了按照要求排列的分数。注意,由于原题可能要求输出的是字符串形式,因此在存储之前可能还需要遍历数组,将每个分数转换回字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值