ZOJ1067 Color Me Less
#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;
}
#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;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2008-10-27 21:59 Phinecos(洞庭散人) 阅读(884) 评论(0) 编辑 收藏 举报