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. Python 简明教程 --- 15,Python 函数

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 测试只能证明程序有错误,而不能证明程序没有错误. -- Edsger Dijkstra 目录 本节我 ...

  2. openstack Rocky 社区版部署1.3 安装OpenStack packages

    1 installing the Rocky release on all nodes. yum install centos-release-openstack-rocky 安装之后,会在/etc/ ...

  3. vue基础入门(2.2)

    2.2.基础指令 2.2.1.什么是指令 指令 (Directives) 是带有 v- 前缀的特殊特性,指令特性的值预期是单个 JavaScript 表达式,指令的职责是,当表达式的值改变时,将其产生 ...

  4. 113资讯网——NGINX 502 Bad Gateway——解决方案

    NGINX 502 Bad Gateway错误出现的原因较多,对于后端连接PHP服务的场景下,常见的原因有php服务响应超时,php进程不足等引起的一类服务器错误. 发生原因: PHP FastCGI ...

  5. 修改git指令alias

    修改git指令alias 嫌打git add, git push 太麻烦,可以修改bash里的 alias,改成'ga','gp'这样的短命令 打开 Git Bash, 创建修改.bashrc文件. ...

  6. springMVC中的HttpSession与Model

    目录 1.1 spring的@MODELATTRIBUTE 2.1 session的概念 3.1 示例 4.1 为什么springmvc框架要使用model这个对象呢? 突然发问:相信很多人在做WEB ...

  7. java语言进阶(三)_List_Set_数据结构_Collections

    主要内容 数据结构 List集合 Set集合 Collections 第一章 数据结构 1.1 数据结构有什么用? 常见的数据结构:堆.栈.队列.数组.链表和红黑树 . 1.2 常见的数据结构 栈 栈 ...

  8. java语言基础(九)_final_权限_内部类

    final关键字 final关键字代表最终.不可改变的. 常见四种用法: 可以用来修饰一个类 可以用来修饰一个方法 还可以用来修饰一个局部变量 还可以用来修饰一个成员变量 1)修饰一个类 public ...

  9. Django---进阶1

    目录 静态文件配置 request对象方法初识 pycharm链接数据库(MySQL) django链接数据库(MySQL) Django ORM 字段的增删改查 数据的增删改查 今日作业 静态文件配 ...

  10. nuxt的使用中碰到的问题

    使用npm run generate生成静态页面部署 如果不是部署在域名的根目录下,则需要在nuxt.config.js中添加 // nuxt.config.js export default { r ...