Problem C: centroid algorithm time limit:1 Sec Memory limit:128 MB
submit:2799 solved:1290
[Submit] [Status] [Web Board] Description
In many applications, a target needs to be positioned. For example, for a point A in an unknown coordinate, assuming that a point is adjacent to n points and that the coordinates of n neighboring points are known, the centroid of n points is preferable to an estimate of the point coordinates.
The so-called centroid, refers to its horizontal axis, the vertical axis of n points of the horizontal mean, the ordinate mean point. That is: assuming the coordinates of N points (x1,y1), (X2,y2), ..., the coordinates of the centroid are ((x1+x2+ ...). /N, (y1+y2+ ...) /n).
Now you need to write 2 classes:
1. Point class: Includes the horizontal and vertical coordinates of a dot and provides appropriate constructors, destructors, and copy constructors, as well as the Getx () and Gety () methods.
2. Graph class
(1) data member point *points; represents a collection of points adjacent to point A.
(2) data member: int numofpoints; Indicates the number of neighboring points.
(3) appropriate constructors, destructors.
(4) Point Getcentroid () method: Used to return centroid points.
Note: assignment operations (=) between objects of the same class do not call the copy constructor.
Input
The input is multiple lines, and the first line m>0 indicates that there are m test cases.
Each test case contains multiple lines. The first line of N>0 indicates that there are n points, followed by the horizontal and vertical coordinates of n points, one row for each point.
Output
See examples.
Sample Input
150 01 12 23 34 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
When you use an object as a function to return a value, a temporary object is generated, and the copy constructor is called. However, in the g++ compiler (which is the compiler used by common code::blocks), the copy constructor is not called if the function return value is a local variable when the object returned by the function assigns a value to another object. Therefore, if you want to implement a call to a copy constructor in this program, you must return an object created using the new operator in Getcentroid instead of a local object that has already been defined. ☆!!!!!!
Append codeappend.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;//must be released, otherwise occupy space and will not be deconstructed 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_); The return *p;//pointer can only exist in this field, cannot be returned, and a copy occurs during the return process. } ~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: centroid algorithm