POJ 2503

本文介绍了一个使用哈希表解决特定数学问题的程序实例。通过两组不同的多项式方程,利用直接定址法实现哈希查找,计算并输出两组方程解的交集数量。

hash

#include <stdio.h>
#include <memory.h>
short hash[25000001];
int main()
{
	int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5;
	while(scanf("%d",&a1)!=EOF)
	{
		memset(hash,0,sizeof(hash));
		int max=0;
		scanf("%d %d %d %d",&a2,&a3,&a4,&a5);
		int sum1;
		for(x1=-50;x1<=50;x1++)
		{
			if(!x1) continue;
			for(x2=-50;x2<=50;x2++)
			{
				if(!x2) continue;
				sum1=a1*x1*x1*x1+a2*x2*x2*x2;
				sum1=-sum1;
				sum1=sum1+12500000;//关键字key

				hash[sum1]++;//直接定址法
			}
		}

		int sum2;
		for(x3=-50;x3<=50;x3++)
		{
			if(!x3) continue;
			for(x4=-50;x4<=50;x4++)
			{
				if(!x4) continue;
				for(x5=-50;x5<=50;x5++)
				{
					if(!x5) continue;
					sum2=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
			        if(sum2<-12500000||sum2>12500000) continue;
					sum2=sum2+12500000;
					if(hash[sum2]) max+=hash[sum2];
			
				}
			}
		}
		printf("%d\n",max);
	}
}


 

转载于:https://www.cnblogs.com/lj-vs-lishimin/archive/2012/06/16/2774392.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值