1.链接地址
2.问题描述
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
输入样例
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
输出样例
(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)
3.解题思路
枚举比较即可
4.算法实现源代码
#include#include using namespace std; struct RGB{ double r; double g; double b;}; int main(){ RGB a[16]; for(int i=0;i<16;i++) { cin>>a[i].r>>a[i].g>>a[i].b; } RGB b[100]; RGB c[100]; double e1,g1,b1,num,l; int cnt=0; while(cin>>e1>>g1>>b1&&(e1!=-1&&g1!=-1&&b1!=-1)) { num=1000; b[cnt].r=e1; b[cnt].g=g1; b[cnt].b=b1; for(int i=0;i<16;i++) { l=sqrt(pow((a[i].r-e1),2)+pow((a[i].g-g1),2)+pow((a[i].b-b1),2)); if(num>l) { num=l; c[cnt].r=a[i].r; c[cnt].g=a[i].g; c[cnt].b=a[i].b; } } cnt++; } for(int i=0;i