以下是使用 C++ 实现的代码:\n\ncpp\n#include <iostream>\n#include <vector>\n#include <cmath>\n#include <iomanip>\n\nusing namespace std;\n\n// 计算两个点的距离\ndouble calculateDistance(int x1, int y1, int x2, int y2) {\n return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));\n}\n\nint main() {\n int n;\n cin >> n;\n\n vector<pair<int, int>> points(n);\n for (int i = 0; i < n; i++) {\n cin >> points[i].first >> points[i].second;\n }\n\n double minDistance = calculateDistance(points[0].first, points[0].second, points[1].first, points[1].second);\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n double distance = calculateDistance(points[i].first, points[i].second, points[j].first, points[j].second);\n if (distance < minDistance) {\n minDistance = distance;\n }\n }\n }\n\n cout << fixed << setprecision(4) << minDistance << endl;\n\n return 0;\n}\n\n\n上述代码首先定义了一个calculateDistance函数,用于计算两个点之间的距离。接下来,读取输入的点的个数和每个点的坐标,并将它们存储在一个vector<pair<int, int>>中。然后,使用两层循环遍历所有点对,并计算它们之间的距离。如果找到了更小的距离,则更新minDistance的值。最后,输出最短距离,保留4位小数。

C++ 实现 最短距离算法 (无函数) - 寻找平面点对最小距离