ZOJ1067 Color Me Less

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

ZOJ1067 Color Me Less

#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;
}

+ 关注