博客
关于我
Problem F: 质心算法
阅读量:796 次
发布时间:2023-03-04

本文共 2702 字,大约阅读时间需要 9 分钟。

在许多应用中,我们需要定位一个目标点A的坐标。假设已知A点与N个点相邻,并且已知这N个相邻点的坐标。我们可以将这N个点的质心作为A点坐标的一个估计值。质心的计算方法是:取N个点的横坐标和纵坐标的平均值,分别作为质心的横坐标和纵坐标。

以下是两个用于实现上述功能的C++类:

Point类

#include 
#include
using namespace std;class Point {private: double x, y;public: Point(double a = 0, double b = 0) : x(a), y(b) { cout << setprecision(2) << fixed << "The Point (" << x << ", " << y << ") is created!\n"; } ~Point() { cout << setprecision(2) << fixed << "A Point (" << x << ", " << y << ") is erased!\n"; } Point(const Point &q) : x(q.x), y(q.y) { cout << setprecision(2) << fixed << "A Point (" << x << ", " << y << ") is copied!\n"; } double getX() { return x; } double getY() { return y; } double setX(double a) { x = a; } double setY(double b) { y = b; }};

Graph类

#include 
#include
using namespace std;class Graph {private: Point *points; int numOfPoints;public: Graph(Point *p, int n) : numOfPoints(n) { points = new Point[numOfPoints]; for (int i = 0; i < numOfPoints; ++i) { points[i] = p[i]; cout << "A graph with " << numOfPoints << " points is created!\n"; } } ~Graph() { delete[] points; cout << "A graph with " << numOfPoints << " points is erased!\n"; } Point *getCentroid() { double sumX = 0, sumY = 0; for (int i = 0; i < numOfPoints; ++i) { sumX += points[i].getX(); sumY += points[i].getY(); } double avgX = sumX / numOfPoints; double avgY = sumY / numOfPoints; return new Point(avgX, avgY); }};

主程序

int main() {    int cases, num;    double x, y;    Point centroid;    cin >> cases;    for (int i = 0; i < cases; ++i) {        cin >> num;        Point *points[num];        for (int j = 0; j < num; ++j) {            cin >> x >> y;            points[j] = new Point(x, y);        }        Graph graph(points, num);        centroid = graph.getCentroid();        cout << setprecision(2) << fixed << "The centroid is (" << centroid.getX() << ", " << centroid.getY() << ").\n";    }    return 0;}

代码解释

  • Point类

    • 包含两个私有成员变量xy,分别表示点的横坐标和纵坐标。
    • 提供四个构造函数:
      • 默认构造函数,初始化xy为0。
      • 拷贝构造函数,用于拷贝其他Point对象,并打印复制信息。
      • 初始化构造函数,接受xy值,并打印创建信息。
    • 提供两个setXsetY方法,用于修改坐标值。
    • 提供两个getXgetY方法,用于获取坐标值。
    • 提供一个析构函数,用于释放内存,并打印删除信息。
  • Graph类

    • 包含一个私有成员变量points,用于存储相邻点的集合。
    • 包含一个私有成员变量numOfPoints,用于存储相邻点的数量。
    • 提供两个构造函数:
      • 初始化构造函数,接受一个Point数组和数量,初始化points成员,并打印创建信息。
      • 拓拱构造函数,释放内存,并打印擦除信息。
    • 提供一个getCentroid方法,计算质心。该方法遍历所有点,计算横坐标和纵坐标的总和,取平均值作为质心的坐标,并返回一个新的Point对象。
    • 提供一个析构函数,释放内存,并打印擦除信息。
  • 主程序

    • 读取输入数据,处理多个测试用例。
    • 对于每个测试用例,读取相邻点的数量和坐标,创建Point对象并存储在数组中。
    • 创建Graph对象,计算并输出质心的坐标。
  • 这个代码实现了对质心的计算和处理,满足用户的需求。每个Point对象在创建、复制和删除时都会打印相应的信息,确保程序的运行情况可追溯。

    转载地址:http://oqxfk.baihongyu.com/

    你可能感兴趣的文章
    PoE三种标准:标准 PoE、PoE+、PoE++,网络工程师必知!
    查看>>
    POI 的使用
    查看>>
    poi 读取单元格为null者空字符串
    查看>>
    poi-tl简介与文本/表格和图片渲染
    查看>>
    pointnet分割自己的点云数据_PointNet解析
    查看>>
    POI实现Excel导入Cannot get a text value from a numeric cell
    查看>>
    POI实现Excel导入时提示NoSuchMethodError: org.apache.poi.util.POILogger.log
    查看>>
    POI实现Excel导出时常用方法说明
    查看>>
    POI导出Excel2003
    查看>>
    POI数据获取及坐标纠偏
    查看>>
    Quartz入门看这一篇文章就够了
    查看>>
    POI解析Excel【poi的坑——空行处理】
    查看>>
    POI:POI+JXL实现xls文件添加水印
    查看>>
    POI:POI实现docx文件添加水印
    查看>>
    POJ 1006
    查看>>
    Quartz中时间表达式的设置-----corn表达式
    查看>>
    poj 1035
    查看>>
    POJ 1061 青蛙的约会 (扩展欧几里得)
    查看>>
    Quartz2.2.1简单使用
    查看>>
    POJ 1080 Human Gene Functions(DP:LCS)
    查看>>