首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

ZOJ-1067 根本运算题

2012-10-30 
ZOJ-1067 基本运算题1067:给出16种RGB值作为基准,将后来的颜色值映射到距离最近的颜色。距离的定义为Exampl

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)



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

#include<stdio.h>#include<iostream>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){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]);}}

热点排行