Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register
北京大学《ACM-ICPC竞赛训练》暑期课面向全球招生。容量有限,报名从速!

简短AC代码,有解释

Posted by yuziquan at 2017-05-18 15:38:09 on Problem 1046
//实质就是让你求离在空间直角坐标系中某个给定的点的距离最近的那个点的坐标,即所谓的map(映射)

//注意:If there are more than one color with the same smallest distance, 
//please output the color given first in the color set.

#include<iostream>
using namespace std;
int f(int a, int b, int c)
{
	return a*a + b*b + c*c;
}
struct color
{
	int R;
	int G;
	int B;
}c[16];
int main()
{
	int x, y, z, i, m, n, l,min;      //min记录所求的点的索引值(数组元素下标)
	for (i = 0; i < 16; i++)
	{
		cin >> x >> y >> z;
		c[i].R = x;
		c[i].G = y;
		c[i].B = z;
	}
	while (cin >> m >> n >> l&&!((m == -1) && (n == -1) && (l == -1)))
	{
		min = 0;
		for (i = 1; i < 16; i++)
		{
			if (f(m - c[i].R, n - c[i].G, l - c[i].B) < f(m - c[min].R, n - c[min].G, l - c[min].B)) min = i;
		}
		cout << "(" << m << "," << n << "," << l << ") maps to (" << c[min].R << "," << c[min].G << "," << c[min].B << ")\n";
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator