OJ 2065 Problem B 点之间的距离

Description

定义Point类,有2个double类型的属性表示点的横坐标和纵坐标。重载其减法运算符,用于计算两个点之间的距离。

定义PointArray类,是由Point类的对象组成的数组,有如下成员函数:

  1. void append(const Point&):将一个点加入到数组中。

  2. double getMaxDis():计算数组中距离最远的一对点之间的距离。

Input

输入一系列点的坐标,每个点的坐标占一行。

Output

见样例。

Sample Input

1 1
2 2
3 3
4 4
5 5

Sample Output

14.1421
5.65685

HINT

PointArray类中要用Point类重载的减法运算符。

Append Code

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;
}

Accepted Code

/**
 *  2020年6月9日15:46:49
 *  OJ计算机科学与技术2019-3,4:程序设计基础(2-2)—实验10
 */

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

class Point {
protected:
    double x, y;
public:
    Point(double X = 0, double Y = 0) : x(X), y(Y) {}
    virtual ~Point() {}
    double operator - (const Point &p) {
        double d = (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y);
        return sqrt(d);
    }

    void set(double d, double d1) {
        x = d; y = d1;
    }
};
class PointArray {
    vector<Point> point;
public:
    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;
    }
};
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值