首页 > 代码库 > poj 1046

poj 1046

<div class="plm" style="text-align: center; font-size: 12pt; color: rgb(51, 51, 51); clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;"><span style="color: green;"> </span>Color Me Less</div><span id="crawlSuccess" class="crawlInfo" style="display: inline;"><strong>Time Limit:</strong><span id="timeLimit">1000</span>MS     <strong>Memory Limit:</strong><span id="memoryLimit">10000</span>KB     <strong>64bit IO Format:</strong><span id="_64IOFormat">%I64d & %I64u</span></span><div id="problem_opt" style="font-size: 12px; margin-top: 10px;"><a target=_blank id="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); background-image: url(http://i.vjudge.net/vjudge/jquery-ui-1.8.16.custom/css/custom-theme/images/ui-bg_glass_75_e3e4f8_1x400.png); background-color: rgb(227, 228, 248); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; zoom: 1; overflow: visible; background-position: 50% 50%; background-repeat: repeat no-repeat;"><span class="ui-button-text" style="display: block; line-height: 1.4; padding: 0.4em 1em;">Submit</span></a> <a target=_blank id="problem_status" href=http://www.mamicode.com/"http://i.vjudge.net/vjudge/contest/view.action?cid=54209#status//D/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); background-image: url(http://i.vjudge.net/vjudge/jquery-ui-1.8.16.custom/css/custom-theme/images/ui-bg_glass_75_e3e4f8_1x400.png); background-color: rgb(227, 228, 248); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; zoom: 1; overflow: visible; text-decoration: none; background-position: 50% 50%; background-repeat: repeat no-repeat;">Status

Description

A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of sixteen RGB color values, and a collection of arbitrary RGB colors to be mapped to their closest color in the target set. For our purposes, an RGB color is defined as an ordered triple (R,G,B) where each value of the triple is an integer from 0 to 255. The distance between two colors is defined as the Euclidean distance between two three-dimensional points. That is, given two colors (R1,G1,B1) and (R2,G2,B2), their distance D is given by the equation >

Sample Output

(0,0,0) maps to (0,0,0)
(255,255,255) maps to (255,255,255)
(253,254,255) maps to (255,255,255)
(77,79,134) maps to (128,128,128)
(81,218,0) maps to (126,168,9)
题意:
给出16行数字,再输入数据,找到前面16行数字中,与之距离最近的点。
思路:
当然可以用结构体来做,但是合理的运用while(1)能更好地解决问题。
代码如下:
#include<stdio.h>
int main()
{
	int i,j,flag=0;
	int a[16],b[16],c[16];
	for(i=0;i<16;i++)
	{
		scanf("%d%d%d",&a[i],&b[i],&c[i]);
	}
	int d,x,y,z,s;
	while(1)
	{
		scanf("%d%d%d",&x,&y,&z);
		if(x==-1&&y==-1&&z==-1)
		break;
		d=(x-a[0])*(x-a[0])+(y-b[0])*(y-b[0])+(z-c[0])*(z-c[0]);
		for(i=1;i<16;i++)
		{
			s=(x-a[i])*(x-a[i])+(y-b[i])*(y-b[i])+(z-c[i])*(z-c[i]);
			if(d>s)
			d=s,flag=i;
		}
		printf("(%d,%d,%d) maps to (%d,%d,%d)\n",x,y,z,a[flag],b[flag],c[flag]);
	}
	
	return 0;
}