poj 1046

来源:互联网 发布:虚拟串口软件 编辑:程序博客网 时间:2024/05/14 19:25
</pre><div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Color Me Less</div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 10000K</td></tr><tr><td><strong>Total Submissions:</strong> 30865</td><td width="10px"> </td><td><strong>Accepted:</strong> 15025</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">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 <center><img src="http://poj.org/images/1046/color.gif" alt="" /></center></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">The input is a list of RGB colors, one color per line, specified as three integers from 0 to 255 delimited by a single space. The first sixteen colors form the target set of colors to which the remaining colors will be mapped. The input is terminated by a line containing three -1 values.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">For each color to be mapped, output the color and its nearest color from the target set. If there are more than one color with the same smallest distance, please output the color given first in the color set.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace;font-size:14px;">0 0 0255 255 2550 0 11 1 1128 0 00 128 0128 128 00 0 128126 168 935 86 34133 41 193128 0 1280 128 128128 128 128255 0 00 1 00 0 0255 255 255253 254 25577 79 13481 218 0-1 -1 -1

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)

Source

Greater New York 2001
题意:
给出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;}

0 0
原创粉丝点击