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;
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章