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. Unity常见的三种数据本地持久化方案

    做游戏的时候常常会有数据配置或者存读档的需求,本文整理了常用的几种解决方案,分别是Unity自带的PlayerPrefs类,XML文件和Json文件. 一. PlayerPrefs 这是Unity自带 ...

  2. 入门大数据---Spark车辆监控项目

    一.项目简介 这是一个车辆监控项目.主要实现了三个功能: 1.计算每一个区域车流量最多的前3条道路. 2.计算道路转换率 3.实时统计道路拥堵情况(当前时间,卡口编号,车辆总数,速度总数,平均速度) ...

  3. ceph集成openstack cinder

    本环境ceph已经搭建,ceph搭建麻烦见本博客的其他文章 1 在cinder-volume节点安装ceph client yum install -y ceph-common 注意:glance要安 ...

  4. day03组件

    1.text   编写文本信息,等价于span 2.view 等价于div 3.image ============================wxml====================== ...

  5. html5中contenteditable 光标_如何设置光标位置

    在js中,光标是一个对象,当你选中某个元素的时候才会出现光标对象.比如:我们点击一个输入框,实际会产生一个选中对象-selection,这个对象我们可以通过indow.getSelection()来获 ...

  6. PTP时钟和NTP时钟同步有什么区别

    PTP时钟 理论上任何PTP时钟都能实现主时钟和从时钟的功能,但一个PTP通信子网内只能有一个主时钟.整个系统中的最优时钟为最高级时钟GMC(Grandmaster Clock),有着最好的稳定性.精 ...

  7. STA树的深度(树型DP)

    STA树的深度 题目大意 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Outpu ...

  8. 阿里云上安装启动nginx 以及在个人电脑上通过公网ip访问遇到的问题

    1.安装依赖包 //一键安装上面四个依赖 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 2.下载并解压安装包 ...

  9. 重学 Java 设计模式:实战模版模式「模拟爬虫各类电商商品,生成营销推广海报场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 黎明前的坚守,的住吗? 有人举过这样一个例子,先给你张北大的录 ...

  10. day11总结

    """1.什么是函数 具备某一功能的工具===>函数 事先准备工具的过程===>函数的定义 遇到应用场景拿来就用=>函数的调用 2.为何要有函数 内置函 ...