【发布时间】:2018-03-12 13:33:24
【问题描述】:
我有一个任务,因为我真的很挣扎:
编写一个程序,将input-week6-ad-q3.txt(200个点的3D坐标)读入数组,报告如下 1.point1与其他点的距离(考虑第1个坐标是point1,第2个point2,,,.) 2.离点1最近的点和点1到最近点的距离。
我尝试过启动它,但目前无法获取坐标,我什至不知道从哪里开始寻找最近的点。任何帮助将非常感激。
这是我目前所得到的:
#include <iostream>
#include <fstream>
using namespace std;
const int MAX = 200;
typedef double x_coord[MAX];
typedef double y_coord[MAX];
typedef double z_coord[MAX];
int main()
{
x_coord x;
y_coord y;
z_coord z;
int count;
ifstream in_stream;
in_stream.open("input-week6-ad-q4-2.txt");
in_stream.get(x, y, z);
for (count = 0; count < MAX; count++)
{
x[count] = x;
in_stream.get(x);
y[count] = y;
in_stream.get(y);
z[count] = z;
in_stream.get(z);
}
in_stream.close();
system("pause");
return 0;
}
输入文件的点布局如下:
Coordinates of many points
x y z
-0.06325 0.0359793 0.0420873
-0.06275 0.0360343 0.0425949
-0.0645 0.0365101 0.0404362
等等。
【问题讨论】:
-
不要将“寻找最近点”视为编程问题。把它想象成一道数学题。一旦你在纸上得到了解决方案,你就可以开始尝试编码了
-
提示——最快的方法不是比较距离;但距离平方!
-
get()返回单个字符,这不是您想要的。in_stream >> x[count];会有所改进。 -
我建议使用结构的集合(向量或数组)。你可以调用结构
Point3d。通常结构数组比并行数组更好(考虑同步问题)。此外,处理器可以将结构的元素加载到缓存行中,这比 3 个数组更容易。