Problem B: 点之间的距离

Problem B: 点之间的距离

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Usher_Ou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值