Sicily 1815. 计算两点间的距离

来源:互联网 发布:python之父谈go 编辑:程序博客网 时间:2024/05/20 03:40

用类写一个求两点距离......

权当练习写下类吧!

// Problem#: 1815// Author#: Reid Chan#include <iostream>#include <iomanip>#include <cmath>using namespace std;class Point {public:    Point(double x, double y) {        this->x = x;        this->y = y;    }    double distance(Point p) {        double r;        r = (x-p.getX()) * (x-p.getX()) + (y-p.getY()) * (y-p.getY());        return sqrt(r);    }    double getX() { return x; }    double getY() { return y; }private:    double x;    double y;};int main() {    int t;    cin >> t;    while (t--) {        double ax, ay, bx, by;        cin >> ax >> ay >> bx >> by;        Point a(ax, ay), b(bx, by);        cout << fixed << setprecision(2) << a.distance(b) << endl;    }    return 0;}                                 


0 0
原创粉丝点击