C++ 点在圆内吗?

  • Post author:
  • Post category:其他





点在圆内吗?

时间限制: 1 Sec 内存限制: 128 MB


题目描述


定义一个Point类和Circle类,用于判断给定的一系列的点是否在给定的圆内。

其中,Point类:

1.有2个成员x和y,分别为其横坐标和纵坐标;1个静态成员numOfPoints,用于计算生成的点的个数。

2.具有构造函数、析构函数和拷贝构造函数,具体格式输出根据样例自行判断。

3. 具有静态方法int getNumOfPoints(),用于返回numOfPoints的值。

4. 具有int getX()和int getY()方法,用于获取横坐标和纵坐标。

Circle类:

1.拥有Point类的对象center,表示圆心坐标。拥有radius对象,表示圆的半径;1个静态成员numOfCircles,用于指示生成了多少个圆对象。

5. 具有构造函数、析构函数和拷贝构造函数,具体格式根据样例自行判断。

3.具有静态方法int getNumOfCircles(),返回numOfCircles的值。

6. 具有getCenter()方法,返回圆心坐标。注意:根据输出结果判断返回值类型。

7. 具有bool pointInCircle(Point &)方法,用于判断给定的点是否在当前圆内。是则返回true,否则返回false。


输入


输入分多行。

第一行M>0,表示有M个测试用例。

每个测试用例又包括多行。第1行包含3个整数,分别为一个圆的横坐标、纵坐标和半径。第2行N>0,表示之后又N个点,每个点占一行,分别为其横坐标和纵坐标。

所有输入均为整数,且在int类型范围内。


输出


输出见样例。注意:在圆的边上的点,不在圆内。


样例输入


2

0 0 10

3

2 2

11 11

10 0

1 1 20

3

2 2

1 1

100 100


样例输出


The Point (0, 0) is created! Now, we have 1 points.

The Point (1, 1) is created! Now, we have 2 points.

A circle at (1, 1) and radius 1 is created! Now, we have 1 circles.

We have 2 points and 1 circles now.

The Point (0, 0) is created! Now, we have 3 points.

A Point (0, 0) is copied! Now, we have 4 points.

A Point (0, 0) is copied! Now, we have 5 points.

A circle at (0, 0) and radius 10 is created! Now, we have 2 circles.

A Point (0, 0) is erased! Now, we have 4 points.

The Point (2, 2) is created! Now, we have 5 points.

(2, 2) is in the circle at (0, 0).

The Point (11, 11) is created! Now, we have 6 points.

(11, 11) is not in the circle at (0, 0).

The Point (10, 0) is created! Now, we have 7 points.

(10, 0) is not in the circle at (0, 0).

A Point (0, 0) is erased! Now, we have 6 points.

A circle at (0, 0) and radius 10 is erased! Now, we have 1 circles.

A Point (0, 0) is erased! Now, we have 5 points.

The Point (1, 1) is created! Now, we have 6 points.

A Point (1, 1) is copied! Now, we have 7 points.

A Point (1, 1) is copied! Now, we have 8 points.

A circle at (1, 1) and radius 20 is created! Now, we have 2 circles.

A Point (1, 1) is erased! Now, we have 7 points.

The Point (2, 2) is created! Now, we have 8 points.

(2, 2) is in the circle at (1, 1).

The Point (1, 1) is created! Now, we have 9 points.

(1, 1) is in the circle at (1, 1).

The Point (100, 100) is created! Now, we have 10 points.

(100, 100) is not in the circle at (1, 1).

A Point (1, 1) is erased! Now, we have 9 points.

A circle at (1, 1) and radius 20 is erased! Now, we have 1 circles.

A Point (1, 1) is erased! Now, we have 8 points.

We have 8 points, and 1 circles.

A circle at (1, 1) and radius 1 is erased! Now, we have 0 circles.

A Point (1, 1) is erased! Now, we have 7 points.

A Point (0, 0) is erased! Now, we have 6 points.


Append Code

int main()
{
    int cases,num;
    int x, y, r, px, py;
    Point aPoint(0,0), *bPoint;
    Circle aCircle(1,1,1);
    cin>>cases;
    cout<<"We have "<<Point::getNumOfPoints()<<" points and "<<Circle::getNumOfCircles()<<" circles now."<<endl;
    for (int i = 0; i < cases; i++)
    {
        cin>>x>>y>>r;
        bPoint = new Point(x,y);
        Circle circle(*bPoint, r);
        cin>>num;
        for (int j = 0; j < num; j++)
        {
            cin>>px>>py;
            if (circle.pointInCircle(*(new Point(px, py))))
            {
                cout<<"("<<px<<", "<<py<<") is in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
            else
            {
                cout<<"("<<px<<", "<<py<<") is not in the circle at (";
                cout<<circle.getCenter().getX()<<", "<<circle.getCenter().getY()<<")."<<endl;
            }
        }
        delete bPoint;
    }
    cout<<"We have "<<Point::getNumOfPoints()<<" points, and "<<Circle::getNumOfCircles()<<" circles."<<endl;
    return 0;
}
#include<iostream>
using namespace std;
class Point
{
private:
    int x,y;
    static int numofpoints;
public:
    friend class Circle;
    Point():x(0),y(0){numofpoints++;cout<<"The Point"<<" "<<'('<<x<<','<<" "<<y<<')'<<" "<<"is"<<" "<<"created!"<<" Now, we have "<<numofpoints<<" points."<<endl;}
    Point(int xx,int yy):x(xx),y(yy){numofpoints++;cout<<"The Point"<<" "<<'('<<x<<','<<" "<<y<<')'<<" "<<"is"<<" "<<"created!"<<" Now, we have "<<numofpoints<<" points."<<endl;}
    Point(const Point &q):x(q.x),y(q.y){numofpoints++;cout<<"A Point"<<" "<<'('<<x<<','<<" "<<y<<')'<<" "<<"is"<<" "<<"copied!"<<" Now, we have "<<numofpoints<<" points."<<endl;}
    ~Point(){numofpoints--;cout<<"A Point"<<" "<<'('<<x<<','<<" "<<y<<')'<<" "<<"is"<<" "<<"erased!"<<" Now, we have "<<numofpoints<<" points."<<endl;}
    static int getNumOfPoints(){return numofpoints;}
    int getX(){return x;}
    int getY(){return y;}
};
class Circle
{
private:
    Point center;
    double radius;
    static int numofcircles;
public:
    friend class Point;
    Circle(int x1,int y1,double r):center(x1,y1),radius(r){numofcircles++;cout<<"A circle at ("<<center.x<<", "<<center.y<<") and radius "<<radius<<" is created! Now, we have "<<numofcircles<<" circles."<<endl;}
    Circle(Point cen,double r):center(cen),radius(r){numofcircles++;cout<<"A circle at ("<<center.x<<", "<<center.y<<") and radius "<<radius<<" is created! Now, we have "<<numofcircles<<" circles."<<endl;}
    Circle(const Circle &q):center(q.center),radius(q.radius){numofcircles++;cout<<"A circle at ("<<center.x<<", "<<center.y<<") and radius "<<radius<<" is copied! Now, we have "<<numofcircles<<" circles."<<endl;}
    ~Circle(){numofcircles--;cout<<"A circle at ("<<center.x<<", "<<center.y<<") and radius "<<radius<<" is erased! Now, we have "<<numofcircles<<" circles."<<endl;}
    static int getNumOfCircles(){return numofcircles;}
    Point &getCenter(){return center;}
    bool pointInCircle(Point &q)
    { if((q.x)>(center.x-radius)&&(q.x)<(center.x+radius)&&(q.y)>(center.y-radius)&&(q.y)<(center.y+radius))
         return true;
      else
         return false;
    }
};
int Point::numofpoints=0;
int Circle::numofcircles=0;



版权声明:本文为tttllld原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。