Problem B: 点之间的距离
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 355 Solved: 258
[ Submit][ Status][ Web Board]
Description
定义Point类,有2个double类型的属性表示点的横坐标和纵坐标。重载其减法运算符,用于计算两个点之间的距离。
定义PointArray类,是由Point类的对象组成的数组,有如下成员函数:
1. void append(const Point&):将一个点加入到数组中。
2. double getMaxDis():计算数组中距离最远的一对点之间的距离。
Input
输入一系列点的坐标,每个点的坐标占一行。
Output
见样例。
Sample Input
1 12 23 34 45 5
Sample Output
14.14215.65685
HINT
PointArray类中要用Point类重载的减法运算符。
Append Code
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
class Point
{
public:
double x,y;
Point(double xx=0,double yy=0):x(xx),y(yy){}
void set(double xx,double yy){x =xx;y=yy;}
friend double operator - (const Point &a,const Point &b)
{
Point p;
p.x = a.x - b.x;
p.y = a.y - b.y;
double ans = sqrt(p.x*p.x+p.y*p.y);
return ans;
}
};
class PointArray
{
public:
vector<Point> point;
PointArray()
{
point.clear();
}
void append(const Point& p)
{
point.push_back(p);
}
double getMaxDis()
{
double d1=0,d2=0;
for(int i=0;i<point.size();i++)
{
for(int j=i+1;j<point.size();j++)
{
d1 = point[i]-point[j];
d2 = max(d1,d2);
}
}
return d2;
}
};
int main()
{
Point p, p1(10, 10), p2(20, 20);
PointArray pArr;
double x, y;
while(cin>>x>>y)
{
p.set(x, y);
pArr.append(p);
}
cout<<p2 - p1<<endl;
cout<<pArr.getMaxDis()<<endl;
return 0;
}