题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2073
说明: 这个题目要求的是A(x1, y1), B(x2, y2)两个点之间的距离,所以可以用(A(x1, y1) - (0,0)) - (B(x2, y2) - (0,0))来求,因此题目就变成了求(x, y)的距离了。其中可以看到,从(0, x) 到(x, 0)增加x个sqrt(2), 从(x, 0) 到(0, x+1)增加sqrt(x*x + (x+1)*(x+1)),对于其他的点就是多了或者少了几个sqrt(2);其中对于一个点除了(0, x)和(x, 0)以外的点(z, t),z+t == x。所以我们合一直接求出(0, x),(x, 0)上面点的值, 然后(z,t)就是减去或者加上相应数量的sqrt(2). 给定(z, t) 首先确定的就是 sqrt(x*x + (x+1)*(x+1))的和,因为我是从(0, x) 到 (x-1, 0)然后加上相应的sqrt(2)所以用的就是sqrt(x*x + (x-1)*(x-1))
#include <stdio.h>
#include <math.h>
double distoorg(int x, int y)
{
int i, s;
double dis = 0;
s = x + y;
dis += s * (s -1) * sqrt(2) / 2;
for(i = s; i > 0; i--)
dis += sqrt(i*i + (i-1)*(i-1));
dis += x*sqrt(2);
return dis;
}
int main()
{
int n, x1, y1, x2, y2;
scanf("%d", &n);
while(n--)
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
printf("%.3lf\n", fabs(distoorg(x1, y1) - distoorg(x2, y2)));
}
return 0;
}