蓝桥杯---平面四点最小距离

来源:互联网 发布:java怎么清理cookie 编辑:程序博客网 时间:2024/06/05 16:17

没什么意思


已知平面上若干个点的坐标。

需要求出在所有的组合中,4个点间平均距离的最小值(四舍五入,保留2位小数)。

比如有4个点:a,b,c,d, 则平均距离是指:ab, ac, ad, bc, bd, cd 这6个距离的平均值。

每个点的坐标表示为:横坐标,纵坐标

坐标的取值范围是:1~1000

例如,如果程序输入:
10,10
20,20
80,50
10,20
20,10

则程序应该输出:
11.38

但是因为空间的限制,最后一组数据始终没有过,所以只能呵呵了。。。

#include <iostream>#include <stdio.h>#include <memory.h>#include <cmath>using namespace std;#define EN cout<<endl;#define  SP cout<<' ';struct T{int x,y;} point[1000];int main(){int count=0,xx,yy;double avr=-1;while(~scanf("%d,%d",&xx,&yy))  point[count].x=xx,point[count++].y=yy;short int distan[1005][1005];for(int i=0;i<count-1;i++) for(int j=i+1;j<count;j++)   distan[i][j]=distan[j][i]=sqrt(pow( (point[i].x-point[j].x),2 )  +  pow((point[i].y-point[j].y),2)  );for(int i=0;i<count-3;i++)  for(int j=i+1;j<count-2;j++){  if(distan[i][j]>avr&&avr!=-1) continue;  for(int x=j+1;x<count-1;x++){  if( (distan[i][x]>avr||distan[j][x]>avr)&&avr!=-1) continue;  for(int y=x+1;y<count;y++){  if( (distan[i][y]>avr||distan[j][y]>avr||distan[x][y]>avr)&&avr!=-1 )continue;        double temp = sqrt(pow( (point[i].x-point[j].x),2 )  +  pow((point[i].y-point[j].y),2)  );        temp+=sqrt(pow( (point[i].x-point[x].x),2 )  +  pow((point[i].y-point[x].y),2)  );        temp+=sqrt(pow( (point[i].x-point[y].x),2 )  +  pow((point[i].y-point[y].y),2)  );        temp+=sqrt(pow( (point[j].x-point[x].x),2 )  +  pow((point[j].y-point[x].y),2)  );        temp+=sqrt(pow( (point[j].x-point[y].x),2 )  +  pow((point[j].y-point[y].y),2)  );        temp+=sqrt(pow( (point[x].x-point[y].x),2 )  +  pow((point[x].y-point[y].y),2)  );        temp/=6;        if(avr==-1||avr>temp) avr=temp;      }      }  }         printf("%.2lf",avr);return 0;}


0 0