1046 Color Me Less

本文介绍了一个简单的颜色匹配算法实现,该算法通过计算输入颜色与预定义颜色集合之间的欧几里得距离来找出最接近的颜色。主要使用了C++进行实现,并通过循环遍历的方式找到与给定颜色最为相似的标准颜色。

水题

  1. //4377084_AC_0MS_392K
  2. #include<iostream>
  3. #include<math.h>
  4. using namespace std;
  5. #define MAXINT 2147483647
  6. struct Color
  7. {
  8.     int r,g,b;
  9. };
  10. Color c[16];
  11. Color temp;
  12. inline double dis(Color x,Color y)
  13. {
  14.     return sqrt((double)(x.r-y.r)*(x.r-y.r)+(x.g-y.g)*(x.g-y.g)+(x.b-y.b)*(x.b-y.b));
  15. }
  16. int main()
  17. {
  18.     while(1)
  19.     {
  20.         for(int i=0;i<16;++i)
  21.         {
  22.             if(scanf("%d%d%d",&c[i].r,&c[i].g,&c[i].b)==-1) return 0;
  23.         }
  24.         while(1)
  25.         {
  26.             scanf("%d%d%d",&temp.r,&temp.g,&temp.b);
  27.             if(temp.r==-1&&temp.g==-1&&temp.b==-1)
  28.             {
  29.                 break;
  30.             }
  31.             double m=MAXINT;
  32.             int t;
  33.             for(int i=0;i<16;++i)
  34.             {
  35.                 double d=dis(temp,c[i]);
  36.                 if(d<m)
  37.                 {
  38.                     m=d;
  39.                     t=i;
  40.                 }
  41.             }
  42.             printf("(%d,%d,%d) maps to (%d,%d,%d)/n",temp.r,temp.g,temp.b,c[t].r,c[t].g,c[t].b);
  43.         }
  44.     }
  45.     return 0;
  46. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值