1067: 16 RGB values are given as the reference, and the subsequent color values are mapped to the nearest color.
Distance is defined
Example
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
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
Output
(0, 0) maps to (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)
Simple question. Calculate the minimum distance in sequence.
C ++ code
# Include <stdio. h>
# Include <iostream>
Using namespace std;
Int color [16] [3];
Int main ()
{
Int R, G, B;
Int index;
Int diff;
Int cal;
// Target color
For (int I = 0; I <16; I ++)
{
Cin> color [I] [0];
Cin> color [I] [1];
Cin> color [I] [2];
}
While (1)
{
Index =-1;
Diff =-1;
Cin> R;
Cin> G;
Cin> B;
If (R =-1 & G =-1 & B =-1)
Break;
For (int I = 0; I <16; I ++)
{
Cal = (R-color [I] [0]) * (R-color [I] [0]) + (G-color [I] [1]) * (G-color [I] [1]) + (B-color [I] [2]) * (B-color [I] [2]);
If (diff =-1 | cal <diff)
{
Diff = cal;
Index = I;
}
}
Printf ("(% d, % d, % d) maps to (% d, % d, % d) \ n", R, G, B, color [index] [0], color [index] [1], color [index] [2]);
}
}