题目分析:本题是求给定的十六个点的集合,然后求给出的点到点集的最短的点。我们只需求三个点之间的平方就可以了,不用用题目的方式开方。
用数组存开始的点。每次访问三个。然后求和。求出每个点的和进行比较,记录较小的点的下标。
注意: int sum, sum0 = 100000000;每次循环都要将sum0进行初始化,否则会出错。
本人小菜鸡一个,感谢您的阅读!
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int arr[48];
for (int i = 0; i < 48; i++)cin >> arr[i];
int a, b, c, x, y, z,j=0;
while (cin >>a >> b >> c) {
int sum, sum0 = 100000000;
int x1, y1, z1;
if (a == -1 && b == -1 && c == -1)break;
for (int i = 0; i < 48; i = i + 3) {
x = i, y = i + 1, z = i + 2;
sum = pow(a - arr[x], 2) + pow(b - arr[y], 2) + pow(c - arr[z], 2);
if (sum < sum0) {
x1 = x, y1 = y, z1 = z;
sum0 = sum;
}
}
//(0,0,0) maps to (0,0,0)
cout << "(" << a << "," << b << "," << c << ") maps to (" << arr[x1] << ',' << arr[y1] << ',' << arr[z1] << ')' << endl;
}
return 0;
}