hdu 1030 Delta-Wave+

关于函数数组的一点学习

    刚做了hdu1030 Delta-Wave,里面的规律颇动人心弦。

http://blog.csdn.net/blogs_of_slicer/article/details/19414395

    不过代码码到一半的时候,我发现这是做了两次同样的一道工序,这一道工序里有三个函数,顺序执行。于是我想,如果能把它们存在一个数组里,不就不用写六遍了么,一个循环就够了。六遍还不多,如果一道工序里有几千个函数,那么用循环就是必然了。

    &then,我查到了这个http://zhidao.baidu.com/link?url=YeflsUYQtp8ReKMrr5M_yb04OWt-IVZhFFq976K00OXnZid9K1Uy2NuWoPZ5ij-KFkpj6FOQPKBtbyWA5mLoPa

    这里面的两种方法都很棒,当时看呆了,顺便就拿1030开刀,试一下这两种函数数组的写法。

method①:

#include<cstdio>
#include<cmath>
#include<cstdlib>
typedef int (*PF_CALC) (int a, int b);
int calc_x(int x,int n)//*
{
	return (int)sqrt((double)n-1)+1;
}
int calc_y(int x,int n)
{
	return (n-(x-1)*(x-1)-1)/2+1;
}
int calc_z(int x,int n)
{
	return (x*x-n)/2+1;
}
int main()
{
	int n,m,a[6];
	PF_CALC pFuncList[3] = {calc_x, calc_y, calc_z};
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(int i=0;i<6;i++)
		{
			a[i]=pFuncList[i%3](a[i/3*3],i<3?m:n);
		}
		int ans=abs(a[0]-a[3])+abs(a[1]-a[4])+abs(a[2]-a[5]);
		printf("%d\n",ans);
	}
	return 0;
}
PS:注意*处,即使x没用,为了能装进数组里也要保持队形啊,下面这种方法就不太涉及这个问题了。


method②:

#include<cstdio>
#include<cmath>
#include<cstdlib>
int calc_x(int n)
{
	return (int)sqrt((double)n-1)+1;
}
int calc_y(int x,int n)
{
	return (n-(x-1)*(x-1)-1)/2+1;
}
int calc_z(int x,int n)
{
	return (x*x-n)/2+1;
}
int all_calc(int flag,int x,int n)
{
	switch(flag%3)
	{
		case 0:return calc_x(n);
		case 1:return calc_y(x,n);
		case 2:return calc_z(x,n);
	}
}
int main()
{
	int n,m,a[6];
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(int i=0;i<6;i++)
		{
			a[i]=all_calc(i,a[i/3*3],i<3?m:n);
		}
		int ans=abs(a[0]-a[3])+abs(a[1]-a[4])+abs(a[2]-a[5]);
		printf("%d\n",ans);
	}
	return 0;
}
PS:亲测AC~~~ 大笑





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值