题目描述
输入
测试数据的组数 t
第一组点的个数
第一个点的 x 坐标 y坐标
第二个点的 x坐标 y坐标
…
输出
输出第一组距离最大的两个点以及其距离
…
在C++中,输出指定精度的参考代码如下:
#include
#include //必须包含这个头文件
using namespace std;
void main( )
{ double a =3.141596;
cout<<fixed<<setprecision(3)<<a<<endl; //输出小数点后3位
输入样例1
2
4
0 0
5 0
5 5
2 10
3
-1 -8
0 9
5 0
输出样例1
Constructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 10.44,between p[1] and p[3].
Distructor.
Distructor.
Distructor.
Distructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 17.03,between p[0] and p[1].
Distructor.
Distructor.
Distructor.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Point
{
private:
double x, y;
public:
Point() : x(0), y(0){}; //缺省构造函数
Point(double x_val, double y_val) : x(x_val), y(y_val) { cout << "Constructor." << endl; }; //有参构造函数
double getX() { return x; };
double getY() { return y; };
void setXY(double x_val, double y_val)
{
x = x_val;
y = y_val;
};
void sexX(double x_val) { x = x_val; };
void sexY(double y_val) { y = y_val; };
double getDisTo(const Point &p); //计算当前点到参数点p的距离
~Point() { cout << "Distructor." << endl; };
};
double Point::getDisTo(const Point &p)
{
return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int x, y, i, j;
Point *p = (Point *)operator new[](n * sizeof(Point));
for (int i = 0; i < n; i++)
{
cin >> x >> y;
new (&p[i]) Point(x, y); // 有参构造函数构造
}
int max1 = 0, max2 = 0;
double max = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (p[i].getDisTo(p[j]) > max)
{
max = p[i].getDisTo(p[j]);
max1 = i;
max2 = j;
}
cout << "The longeset distance is " << fixed << setprecision(2) << max;
cout << ",between p[" << max1 << "] and p[" << max2 << "]." << endl;
for (int i = 0; i < n; i++)
p[i].~Point(); //析构对象
operator delete(p); //释放空间
}
return 0;
}