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运算符创建的对象,而不是一个已经定义的局部对象。

主函数:

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;
}
解析:
自己修改一下输出格式,分析一下输出,第一行为主函数的构造函数,后五行为数组的初始化,后五行是输入产生的构造函数,后五行的构造函数是由Graph中的数组产生的,后边的拷贝构造函数是由返回类型是类导致的。第一轮五个erased是由delete数组产生的,即主函数中的Points,第二轮的五个erased是由Graph的本身数组产生的
如果想在此程序中实现拷贝构造函数的调用,必须在getCentroid中返回一个使用new运算符创建的对象,而不是一个已经定义的局部对象。
代码:
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
class Point
{
private:
    double x,y;
    friend class Graph;
public:
    Point(double xx=0,double yy=0):x(xx),y(yy)
    {
        cout<<"The Point ("<<x<<", "<<y<<") is created!"<<endl;
    }
    Point(Point& p):x(p.x),y(p.y)
    {
        cout<<"A Point ("<<x<<", "<<y<<") is copied!"<<endl;
    }
    double getX()
    {
        return x;
    }
    double getY()
    {
        return y;
    }
    ~Point()
    {
        cout<<"A Point ("<<x<<", "<<y<<") is erased!"<<endl;
    }
};
class Graph
{
private:
    int num;
    Point* ps;
public:
    Graph(Point *pss,int n):num(n)
    {
        ps=new Point[num];
        for(int i=0;i<num;i++)
            ps[i]=pss[i];
    }
    Point getCentroid()//调用拷贝构造函数
    {
        double xx=0;
        double yy=0;
        for(int i=0;i<num;i++)
        {
            xx+=ps[i].x;
            yy+=ps[i].y;
        }
        xx/=num;
        yy/=num;
        Point *pp;
        pp=new Point(xx,yy);//调用构造函数,必须new
        return *pp;
    }//返回之后pp被析构掉
    ~Graph()
    {
        delete []ps;//先析构主函数的Point
        cout<<"A graph with "<<num<<" points is erased!"<<endl;//再析构Graph的数组
    }
};

Problem H: 质心算法的更多相关文章

  1. Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

    Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  2. Problem H: 小姐姐的QQ号(DFS)

    Contest - 河南省多校连萌(四) Problem H: 小姐姐的QQ号 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 297  Solved:  ...

  3. 数据结构实习 Problem H 迷宫的最短路径

    数据结构实习 Problem H 迷宫的最短路径 题目描述 设计一个算法找一条从迷宫入口到出口的最短路径. 输入 迷宫的行和列m n 迷宫的布局 输出 最短路径 样例输入 6 8 0 1 1 1 0 ...

  4. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

  5. The Ninth Hunan Collegiate Programming Contest (2013) Problem H

    Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...

  6. Gym 100531H Problem H. Hiking in the Hills 二分

    Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...

  7. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  8. 清北学堂入学测试P4751 H’s problem(h)

    P4751 H’s problem(h)  时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 小H是一个喜欢逛街的女孩子,但是由于上了大学 ...

  9. Problem H

    Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个 ...

随机推荐

  1. Linux下9种优秀的代码比对工具推荐

    大家好,我是良许. 在我们编写代码的时候,我们经常需要知道两个文件之间,或者同一个文件不同版本之间有什么差异性.在 Windows 下有个很强大的工具叫作 BeyondCompare ,那在 Linu ...

  2. 如何在一个HTML文件中嵌套另一个HTML文件并且可以进行切换HTML文件

    使用iframe 要点:a标签+iframe A标签的target属性 iframe 的id与name属性 示例: <!DOCTYPE html> <html> <hea ...

  3. Executors框架之ScheduledExecutorService实现定时任务

    一.简介 An ExecutorService that can schedule commands to run after a given delay, or to execute periodi ...

  4. Scrapy框架简介及小项目应用

    今天来总结一下Scrapy框架的用法.scrapy的架构如下: Engine  :引擎,处理整个系统的数据流处理.触发事务,是整个框架的核心. Items :项目,它定义了爬取结果的数据结构,爬取的数 ...

  5. sorted 函数及小练习

    python 中sorted函数 sorted() 函数对所有可迭代的对象进行排序操作. sorted 语法: sorted(iterable[, cmp[, key[, reverse]]]) 参数 ...

  6. python编程从入门到实践笔记

    我的第一个hello world 程序 print("hello python world") print("hello python world"*3) 打印 ...

  7. HTML5(四)Drag and Drop

    HTML5 拖放(Drag 和 Drop) 拖放 拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 设置元素为可拖放 首先,为了使元素 ...

  8. gulp之demo

    1.安装gulp cnpm install -g gulp; 2.然后还需要在当前目录安装gulp,具体不详,只知道安装了之后会在当前目录下的node_modules下多一个gulp文件夹 cnpm ...

  9. Ticket Game CodeForces - 1215D 博弈题

    题目描述 Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even ...

  10. python分块读取大数据,避免内存不足