/* @问题:计算折线路径距离(无限的路) @Time:20160416 @Author:Alex @分析:这个线段距离原点的长可分为两部分。 第一部分是无点的线段,长度依次为√(0^2+1^2)、√(1^2+2^2)、√(2^2+3^2)……√((n-1)^2+n^2)这个n的值刚好为这一点的横纵坐标之和; 第二部分是有点的线段,这个很容易发现长度依次为√2、2√2、3√2、……(m-1)√2 ,第m条有点线段长度是这一点横坐标乘以√2.并且这个m的值与n相等. */ #include<stdio h=""> #include<math h=""> typedef struct positionType { int x; int y; } positive; double Cal_distoorg(positive A) { int count=0,step=0; double Adistoorg=0;//A到原点的距离 //A点 step = A.x+A.y; Adistoorg += step*(step-1)*sqrt((double)2)/2; for(count = step;count > 0;count--) { Adistoorg += sqrt((double)(count*count+(count-1)*(count-1))); } Adistoorg += A.x*sqrt((double)2); return Adistoorg; } int main() { int N; positive A,B; double distance=0; while(scanf("%d",&N)!=EOF && (N<=100)) { while(N--) { scanf("%d%d%d%d",&A.x,&A.y,&B.x,&B.y); distance = fabs(Cal_distoorg(A)-Cal_distoorg(B)); printf("%.3lf\n",distance); } } return 0; } </math></stdio>