aa
#include<iostream>
#include <iomanip>
#include<math.h>
using namespace std;
class point
{
double x, y;
public:
point();
point(double x_value, double y_value);
~point();
double getX();
double getY();
void setXY(double x1, double y1) { x = x1; y = y1; }
void setX(double x_value);
void setY(double y_value);
double getDisTo(point& p);
};
point::point()
{
x = 0;
y = 0;
cout << "Constructor." << endl;
}
point::point(double x_value, double y_value)
{
x = x_value;
y = y_value;
cout << "Constructor." << endl;
}
point::~point()
{
cout << "Distructor." << endl;
}
double point::getX()
{
return x;
}
double point::getY()
{
return y;
}
void point::setX(double x_value)
{
x = x_value;
}
void point::setY(double y_value)
{
y = y_value;
}
double point::getDisTo(point& p)
{
return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
int main()
{
int t, i1, i2, i, j;
double max = 0, x, y;
cin >> t;
while (t--)
{
int ts = 0;
cin >> ts;
point* p = new point[ts];
for (i = 0; i < ts; i++)
{
cin >> x;
cin >> y;
p[i].setXY(x, y);
}
for (i = 0; i < ts; i++)
{
for (j = i + 1; j < ts; j++)
{
if (max < p[i].getDisTo(p[j]))
{
max = p[i].getDisTo(p[j]);
i1 = i;
i2 = j;
}
}
}
cout << "The longeset distance is " << fixed << setprecision(2) << max
<< ",between p[" << i1 << "] and p[" << i2 << "]." << endl;
delete[] p;
}
return 0;
}
该代码展示了如何在C++中定义一个Point类,用于存储二维坐标,并实现构造函数、析构函数以及计算两点之间的距离。程序通过main函数输入点集,找出最长距离及其对应的点索引。

790

被折叠的 条评论
为什么被折叠?



