poj1046 枚举

题意:给定16个点作为目标点,然后任给出一点p,根据距离公式,计算距点p最近的目标点。

算法:枚举 


#include <iostream>
using namespace std;

int r,g,b;
int target[16][3];

int main()
{
	double d, ans;
	int key;
	for (int i=0; i<16; i++)
	{
		cin >> target[i][0] >> target[i][1] >> target[i][2];
	}
	while (cin >> r >> g >> b)
	{
		if (r == -1 && g == -1 && b == -1)
		{
			break;
		}
		ans = 255*255*3+10;
		for (int i=0; i<16; i++)
		{
			d = (target[i][0]-r)*(target[i][0]-r) + (target[i][1]-g)*(target[i][1]-g) + (target[i][2]-b)*(target[i][2]-b);
			if (d < ans)
			{
				ans = d;
				key = i;
			}
		}
		cout << "(" << r << "," << g << "," << b << ") maps to (" << target[key][0] << "," << target[key][1] << "," << target[key][2] << ")" << endl;
	}
}



你可能感兴趣的:(枚举,poj)