https://hihocoder.com/problemset/problem/1696
求折线中点,一开始想成先求横坐标中点了,肯定是错的。
一定要从线段长度求中点,然后中点公式推了好久。。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n;
typedef struct{
int x, y;
}Node;
Node node[];
double a[];
double dist(int a, int b)
{
return sqrt(a*a+b*b);
}
int main()
{
double sum = ;
cin >> n;
for(int i = ; i < n; i++){
cin >> node[i].x >> node[i].y;
}
for(int i = ; i < n-; i++){
sum += dist(node[i+].x-node[i].x, node[i+].y-node[i].y);
a[i] = sum;//存放到点i+1为止的折线长度
}
sum /= ;
int i;
for(i = ; i < n-; i++){
if(sum < a[i]||sum-a[i]<1e-){//小于或等于
break;
}
}
sum -= a[i-];
double tx = node[i+].x-node[i].x;
double ty = node[i+].y-node[i].y;
double midx = sum/dist(node[i+].x-node[i].x, node[i+].y-node[i].y)*tx+node[i].x;
double midy = sum/dist(node[i+].x-node[i].x, node[i+].y-node[i].y)*ty+node[i].y;
printf("%.1lf %.1lf\n", midx, midy);
return ;
}