设为首页 加入收藏

TOP

ZOJ-1067基本运算题
2014-11-23 22:57:33 来源: 作者: 【 】 浏览:4
Tags:ZOJ-1067 基本 运算

1067:给出16种RGB值作为基准,将后来的颜色值映射到距离最近的颜色。

距离的定义为

Example

Input

0 0 0

255 255 255

0 0 1

1 1 1

128 0 0

0 128 0

128 128 0

0 0 128

126 168 9

35 86 34

133 41 193

128 0 128

0 128 128

128 128 128

255 0 0

0 1 0

0 0 0

255 255 255

253 254 255

77 79 134

81 218 0

-1 -1 -1

Output

(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)

简单题。依次计算找距离最小即可。

C++代码

#include

#include

using namespace std;

int color[16][3];

int main()

{

int R,G,B;

int index;

int diff;

int cal;

//目标颜色

for(int i=0;i<16;i++)

{

cin>>color[i][0];

cin>>color[i][1];

cin>>color[i][2];

}

while(1)

{

index=-1;

diff=-1;

cin>>R;

cin>>G;

cin>>B;

if(R==-1&&G==-1&&B==-1)

break;

for(int i=0;i<16;i++)

{

cal=(R-color[i][0])*(R-color[i][0])+(G-color[i][1])*(G-color[i][1])+(B-color[i][2])*(B-color[i][2]);

if(diff==-1||cal

{

diff=cal;

index=i;

}

}

printf("(%d,%d,%d) maps to (%d,%d,%d)\n",R,G,B,color[index][0],color[index][1],color[index][2]);

}

}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言可变参数的简单理解-计算最.. 下一篇基于顺序存储的多叉树实现: (4) ..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: