无限的路

Description

甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直线,于是他就在平面直角坐标系中画出如下的图形:       
甜甜的好朋友蜜蜜发现上面的图还是有点规则的,于是他问甜甜:在你画的图中,我给你两个点,请你算一算连接两点的折线长度(即沿折线走的路线长度)吧。      
       

Input

第一个数是正整数N(≤100)。代表数据的组数。        每组数据由四个非负整数组成x1,y1,x2,y2;所有的数都不会大于100。       
       

Output

对于每组数据,输出两点(x1,y1),(x2,y2)之间的折线距离。注意输出结果精确到小数点后3位。      
       

Sample Input

5 0 0 0 1 0 0 1 0 2 3 3 1 99 99 9 9 5 5 5 5
              

Sample Output

1.000 2.414 10.646 54985.047 0.000
 
 
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 double ans[220][220];
 6 int main()
 7 {
 8     for(int j = 1; j < 200; j++)
 9     {
10         for(int i = 0; i <= j; i++)
11         {
12             if(i == 0)
13                 ans[i][j] = ans[j-1][i] + sqrt(1.0*j*j+1.0*(j-1)*(j-1));
14             else
15                 ans[i][j-i] = ans[i-1][j-i+1] + sqrt(2.0);
16         }
17     }
18     int N;
19     int x1,y1,x2,y2;
20     cin >> N;
21     while(N--)
22     {
23         cin >> x1 >> y1 >> x2 >> y2;
24         printf("%.3lf
",fabs(ans[x1][y1]-ans[x2][y2]));
25     }
26 
27     return 0;
28 }
原文地址:https://www.cnblogs.com/wangmengmeng/p/4552738.html