• 实验8:Problem C: 质心算法


    注明一点:这个代码不是我写的,是我跟别人要的,我的程序一直没得到想要的输出结果,水平有限,实在不知道错误在哪

    Home Web Board ProblemSet Standing Status Statistics
     
    Problem C: 质心算法

    Problem C: 质心算法

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 649  Solved: 301
    [Submit][Status][Web Board]

    Description

    在很多应用中,需要对某个目标进行定位。比如对于一个未知坐标的点A,假定已知A点与N个点相邻,且已知N个相邻点的坐标,则可取N个点的质心作为A点坐标的一个估计值。

    所谓质心,就是指其横坐标、纵坐标分别为N个点的横坐标平均值、纵坐标平均值的点。即:假定N个点的坐标分别(x1,y1),(x2,y2),......,则质心的坐标为((x1+x2+...)/N, (y1+y2+...)/N)。

    现在需要你编写2个类:

    1. Point类:包括一个点的横坐标和纵坐标,并提供适当的构造函数、析构函数和拷贝构造函数,以及getX()和getY()方法。

    2. Graph类

    (1)数据成员Point *points;表示与A点相邻的点的集合。

    (2)数据成员:int numOfPoints;表示相邻点的个数。

    (3)适当的构造函数、析构函数。

    (4)Point getCentroid()方法:用于返回质心点。

    注意:同一类的对象之间的赋值运算(=)不调用拷贝构造函数。

    Input

    输入为多行,第一行M>0表示有M个测试用例。

    每个测试用例包含多行。第一行N>0表示有N个点,之后是N个点的横坐标和纵坐标,每个点占一行。

    Output

    见样例。

    Sample Input

    1 5 0 0 1 1 2 2 3 3 4 4

    Sample Output

    The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (1.00, 1.00) is created! The Point (2.00, 2.00) is created! The Point (3.00, 3.00) is created! The Point (4.00, 4.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! The Point (0.00, 0.00) is created! A graph with 5 points is created! The Point (2.00, 2.00) is created! A Point (2.00, 2.00) is copied! A Point (2.00, 2.00) is erased! The centroid is (2.00, 2.00). A Point (4.00, 4.00) is erased! A Point (3.00, 3.00) is erased! A Point (2.00, 2.00) is erased! A Point (1.00, 1.00) is erased! A Point (0.00, 0.00) is erased! A graph with 5 points is erased! A Point (4.00, 4.00) is erased! A Point (3.00, 3.00) is erased! A Point (2.00, 2.00) is erased! A Point (1.00, 1.00) is erased! A Point (0.00, 0.00) is erased! A Point (2.00, 2.00) is erased!

    HINT

     当使用对象作为函数返回值时,会产生一个临时对象,此时会调用拷贝构造函数。但是在g++编译器(也就是大家常用的code::blocks所用的编译器)中,当函数返回的对象给另一个对象进行赋值时,如果函数返回值是一个局部变量,则不会调用拷贝构造函数。所以,如果想在此程序中实现拷贝构造函数的调用,必须在getCentroid中返回一个使用new运算符创建的对象,而不是一个已经定义的局部对象。

    Append Code

    [Submit][Status][Web Board]
    #include<iostream>
    #include<iomanip>
    using namespace std;
    class Point{
    private:
        double x,y;
    public:
        Point():x(0),y(0){cout<<"The Point ("<<setprecision(2)<<fixed<<x<<", "<<setprecision(2)<<fixed<<y<<") is created!"<<endl;}
        Point(double a,double b):x(a),y(b){cout<<"The Point ("<<setprecision(2)<<fixed<<x<<", "<<setprecision(2)<<fixed<<y<<") is created!"<<endl;}
        Point(const Point& p ):x(p.x),y(p.y){cout<<"A Point ("<<setprecision(2)<<fixed<<x<<", "<<setprecision(2)<<fixed<<y<<") is copied!"<<endl;}
        ~Point(){cout<<"A Point ("<<setprecision(2)<<fixed<<x<<", "<<setprecision(2)<<fixed<<y<<") is erased!"<<endl;}
         double getX(){return x;}
         double getY(){return y;}
         void setX(double a){x=a;}
         void setY(double a){y=a;}
    };
    class Graph{
    private:
        Point *points;
        int numOfPoints;
    public:
        Graph():points(NULL),numOfPoints(0){cout<<"A graph with "<<numOfPoints<<" points is created!"<<endl;}
        Graph(Point *p,int a)
        {
            points=new Point[a];
            for(int i=0;i<a;i++)
            {
                points[i]=p[i];
            }
            numOfPoints=a;
            cout<<"A graph with "<<numOfPoints<<" points is created!"<<endl;
        }
        ~Graph(){delete []points;cout<<"A graph with "<<numOfPoints<<" points is erased!"<<endl;}
        Point getCentroid()
        {
            int i;
            double m=0,n=0;
            for(i=0;i<numOfPoints;i++)
            {
                m+=points[i].getX();
                n+=points[i].getY();
            }
            m=m/numOfPoints;
            n=n/numOfPoints;
            Point *p=new Point(m,n);
            return *p;
        }
    };
    
    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()<<")."<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Linux rm命令详解
    标准时间格式("%Y-%m-%dT%H:%M:%S")转化(基于python 3.6)
    通过load json文件读取json指定数据(基于python 3.6)
    遍历win10文件夹并解析json文件,按照json格式存入mongo数据库(基于python 3.6)
    mongo的备份数据库导入现有数据库
    python 获取网页内容新增网页分类+删除指定后缀数组元素功能(基于python 3.6)
    sqlite3的安装和使用(基于python3.5)
    python 获取提交表单网址内容(即需要密码网址)以财务网站为例
    python 分析PDF文件 (基于使用pdf2htmlEX.exe python3.6)
    python 复制多个文件到指定目录(基于python 3.X)
  • 原文地址:https://www.cnblogs.com/auto1945837845/p/5426468.html
Copyright © 2020-2023  润新知