计蒜客_计算坐标重心

方法:枚举所有点间距离

在二维平面上有很多点,这些点的重心定义为:到其他所有点的距离的平均值最小的一个点。

你需要输出这个距离其他点的平均值最小的点的编号,所有点的编号从 11 开始。如果存在多个平均值最小的点,输出编号最大的那一个。(距离为欧几里德距离)

输入格式

第一行输入一个整数 n(1 \le n \le 1000)n(1n1000)

接下来一共 nn 行,每行两个整数,表示每个点的横坐标和纵坐标 x,y(-1000 \le x,y \le 1000)x,y(1000x,y1000)

输出格式

输出和其他点的距离平均值最小的点的编号,如果有多个,输出编号最大的那一个。

样例输入1

2
-617 164
905 -108

样例输出1

2

样例输入2

4
-154 -293
-111 -170
360 -807
-850 862

样例输出2

2

#include <bits/stdc++.h> 
using namespace std;
const double inf = 1e9+7; 
double a[1005][1005];
struct node
{
	double x,y;
}spe[1005];

double length(int a,int b)
{
	double sum = (spe[a].x-spe[b].x)*(spe[a].x-spe[b].x)+(spe[a].y-spe[b].y)*(spe[a].y-spe[b].y);
	return sqrt(sum);
}
int main()
{
	memset(a,0,sizeof a);
	int n;
	scanf("%d",&n);
	for(int i = 1; i <= n; i++)
		scanf("%lf%lf",&spe[i].x,&spe[i].y);
	for(int i = 1; i <= n; i++)
		for(int j = i+1; j <=n; j++)
		{
			if(i!=j)
			{
				a[i][j] = length(i,j);
				a[j][i] = a[i][j];
			}
		}
	int mark;
	double maxn = inf;
	for(int i = 1; i <= n; i++)
	{
		double sum = 0;
		for(int j = 1; j <= n; j++)
			if(i != j)
				sum+=a[i][j];
		if(sum <= maxn)
		{
			maxn = sum;
			mark = i;
		}
	}
	printf("%d\n",mark);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nothing_227/article/details/81065928