1. 程式人生 > >Problem C: 質心算法

Problem C: 質心算法

enter 測試用例 運算符 構造函數的調用 平均值 mst 對象 light 另一個

Problem C: 質心算法

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 2799 Solved: 1290
[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

append.cc,
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;
}

  

#include <iostream>
#include <iomanip>
using namespace std;
class Point
{
public :
    double x, y;
    Point():x(0), y(0)
    {
        cout<<fixed<<setprecision(2)<<"The Point ("<<x<<", "<<y<<") is created!"<<endl;
    }
    Point(double a,double b):x(a), y(b)
    {
        cout<<"The Point ("<<x<<", "<<y<<") is created!"<<endl;
    }
    Point(const 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
{
public :
    Point *points;//必須要釋放,否則占用空間且不會被析構
    int numOfPoints;
    Graph(Point *p, int n):numOfPoints(n)
    {
        points= new Point[n];
        for(int i=0; i<n; i++)
        {
            points[i]=p[i];
        }
        cout<<"A graph with "<<numOfPoints<<" points is created!"<<endl;
    }
    Point getCentroid()
    {
        double x_=0, y_=0;
        for(int i=0; i<numOfPoints; i++)
        {
            x_+=points[i].x;
            y_+=points[i].y;
        }
        x_/=numOfPoints;
        y_/=numOfPoints;
        Point *p=new Point(x_,y_);
        return *p;//指針只能存在於此域中,無法返回,返回過程中會發生拷貝。
    }
    ~Graph()
    {
        delete []points;
        cout<<"A graph with "<<numOfPoints<<" points is erased!"<<endl;
    }
};
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;
}

  

Problem C: 質心算法