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 equationInput
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.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 -1Sample 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行之后输入的就是要比较的 了。 题目解析: 这个题是一个非常简单的枚举题,这个题也可以换一个思路去理解,那么就是把每一行的三个数看作一个点,求空间点的最短距离 这样就很好理解了把,就是用简单的枚举就可以做出来,每一个去比较就可以得出答案。直接看下面的代码把; 代码: #include<iostream> #include<cmath> using namespace std; const int maxn=20; int i,j,k; int main() { int temp; double d,min_d; int a[maxn],b[maxn],c[maxn]; int n,m,p; for(i=0; i<16; i++) { cin>>a[i]>>b[i]>>c[i]; } while(cin>>n>>m>>p&&(n!=-1&&m!=-1&&p!=-1)) { min_d=100000; for(k=0; k<16; k++) { d=sqrt(1.0*((n-a[k])*(n-a[k])+(m-b[k])*(m-b[k])+(p-c[k])*(p-c[k]))); if(min_d>d) { min_d=d; temp=k; } } cout<<"("<<n<<","<<m<<","<<p<<")"; cout<<" maps to "; cout<<"("<<a[temp]<<","<<b[temp]<<","<<c[temp]<<")"<<endl; } return 0; }