hihocoder1696 折线中点(几何)

https://hihocoder.com/problemset/problem/1696

求折线中点,一开始想成先求横坐标中点了,肯定是错的。

一定要从线段长度求中点,然后中点公式推了好久。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<map>
 8 #define lson l, m, rt<<1
 9 #define rson m+1, r, rt<<1|1
10 #define IO ios::sync_with_stdio(false);cin.tie(0);
11 #define INF 0x3f3f3f3f
12 typedef long long ll;
13 using namespace std;
14 int n;
15 typedef struct{
16     int x, y;
17 }Node;
18 Node node[110];
19 double a[110];
20 double dist(int a, int b)
21 {
22     return sqrt(a*a+b*b);
23 }
24 int main()
25 {
26     double sum = 0;
27     cin >> n;
28     for(int i = 0; i < n; i++){
29         cin >> node[i].x >> node[i].y;
30     }
31     for(int i = 0; i < n-1; i++){
32         sum += dist(node[i+1].x-node[i].x, node[i+1].y-node[i].y);
33         a[i] = sum;//存放到点i+1为止的折线长度 
34     }
35     sum /= 2;
36     int i;
37     for(i = 0; i < n-1; i++){
38         if(sum < a[i]||sum-a[i]<1e-6){//小于或等于 
39             break;
40         }
41     }
42     sum -= a[i-1];
43     double tx = node[i+1].x-node[i].x;
44     double ty = node[i+1].y-node[i].y;
45     double midx = sum/dist(node[i+1].x-node[i].x, node[i+1].y-node[i].y)*tx+node[i].x;
46     double midy = sum/dist(node[i+1].x-node[i].x, node[i+1].y-node[i].y)*ty+node[i].y;
47     printf("%.1lf %.1lf\n", midx, midy);
48     return 0;
49 }

猜你喜欢

转载自www.cnblogs.com/Surprisezang/p/8972690.html