题目:
Input
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.
Output
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.
If there are more than one color with the same smallest distance, please output the color given first in the color set.
Sample Input
0 0 0 255 255 255 0 0 1 1 1 1 128 0 0 0 128 0 128 128 0 0 0 128 126 168 9 35 86 34 133 41 193 128 0 128 0 128 128 128 128 128 255 0 0 0 1 0 0 0 0 255 255 255 253 254 255 77 79 134 81 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)
Answer:
#include <iostream>
using std::cout ;
using std::cin ;
using std::endl ;
#include <cmath>
int main()
{
int a[16][3] = {0};
int b[100][3] = {0} ;
int size = 0 ;
int lowest = 65025 ;
double result = 0 ;
int index = 0 ;
for(int i=0 ;i<16;i++)
{
for(int j = 0 ; j<3 ;j++)
{
cin >> a[i][j] ;
}
}
for(int k = 0 ; ;k++)
{
for(int m = 0 ; m <3 ; m++)
{
cin >> b[k][m] ;
}
if(b[k][0]==-1&&b[k][1]==-1&&b[k][2]==-1)
{
break ;
}
size++ ;
}
for(int p = 0 ; p < size ; p++)
{
for(int q = 0 ;q < 16 ;q++)
{
double temp = 0.0 ;
temp = ((b[p][0]-a[q][0])*(b[p][0]-a[q][0])+(b[p][1]-a[q][1])*(b[p][1]-a[q][1])+(b[p][2]-a[q][2])*(b[p][2]-a[q][2]));
result = sqrt(temp);
if(result < lowest)
{
lowest = result ;
index = q ;
}
}
cout << "("<<b[p][0]<<","<<b[p][1]<<","<<b[p][2]<<") maps to ("<<a[index][0]<<","<<a[index][1]<<","<<a[index][2]<<")" << endl ;
lowest = 65025 ;
}
return 0 ;
}