复制代码#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
const int MAXSIZE = 100;
int pos[100];//记录对应的最小值所在位置
struct RGB
{//颜色结构体
double red;
double green;
double blue;
}colors[MAXSIZE];
double distance(RGB* color1,RGB* color2)
{//计算两个颜色的距离
double r = color1->red-color2->red;
double g = color1->green-color2->green;
double b = color1->blue-color2->blue;
return sqrt(r*r+g*g+b*b);
}
int main(void)
{
double r,g,b,dist;
int count=0,i,j,k;
while (cin>>r>>g>>b&&(r!=-1&&g!=-1&&b!=-1))
{
colors[count].red = r;
colors[count].green = g;
colors[count].blue = b;
++count;
}
double min;
k = 0;
for (i=16;i<count;++i)
{
min = numeric_limits<double>::max();//记录最小值
for (j=0;j<16;++j)
{
dist = distance(&colors[i],&colors[j]);
if (dist<min)
{//距离比当前最小值要小
min = dist;
pos[k] = j;//记录最小值位置
}
}
k++;
}
for (i=16,k=0;i<count;++i,++k)
{
cout<<'('<<colors[i].red<<','<<colors[i].green<<','<<colors[i].blue<<')'<<" maps to ";
cout<<'('<<colors[pos[k]].red<<','<<colors[pos[k]].green<<','<<colors[pos[k]].blue<<')'<<endl;
}
return 0;
}
复制代码
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/10/27/1320767.html,如需转载请自行联系原作者