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. Javascript的单线程和异步编程

    运行时概念 下面的内容解释了一个理论上的模型.现代 JavaScript 引擎着重实现和优化了描述的几个语义. 可视化描述 栈 函数调用形成了一个栈帧. function foo(b) { var a ...

  2. python利用列表文件遍历

    关键词:文件遍历/列表 思路:先制作目标文件列表(txt/csv...均可),再逐行读取列表文件 1. 制作列表 linux 终端输入:# find ./abc -type f > list.t ...

  3. 前端基础:HTTP 协议详解

    参考:https://kb.cnblogs.com/page/130970/#httpmeessagestructe HTTP协议是无状态的 http协议是无状态的,同一个客户端的这次请求和上次请求是 ...

  4. sql 语句和实例

    修改字段格式的sql语句: alter table tablename alter column colname newDataType 比如:alter table mytable alter co ...

  5. 2020年学习目标之一——emacs

    这两天在虚机里面安装了centos7(gnome),决定后续自己的学习一直在这个里面进行,对于编辑器我最后选择了emacs,新手一枚,不过正好也算是今年的一项学习目标吧,加油! (完)

  6. python中lambda匿名函数与函数之间的关系

  7. Tomcat Filter之动态注入

    前言 最近,看到好多不错的关于"无文件Webshell"的文章,对其中利用上下文动态的注入Filter的技术做了一下简单验证,写一下测试总结,不依赖任何框架,仅想学习一下tomca ...

  8. 小程序被冻结,忘记原始ID,如何找回?

     登录成功,提示被冻结,选择"账号找回": 阅读须知:账号类型选择“小程序”,需要输入小程序的原始ID,此时已经不记得了~~ 微信:搜索 “ 公众平台安全助手 ” 并关注  点击查 ...

  9. POJ2393贪心

    题意:奶牛们收购了一家世界著名的酸奶工厂Yucky Yogurt. 在接下来的 N (1 <= N <= 10,000) 周,牛奶和人工的价格每周会波动,以致于第i周需要花公司 C_i ( ...

  10. BUUCTF-Misc-No.4

    比赛信息 比赛地址:Buuctf靶场 内心os(蛮重要的) 我只想出手把手教程,希望大家能学会然后自己也成为ctf大佬,再来带带我QWQ 被偷走的文件 | SOLVED | foremost分离一下文 ...