一、Description

A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of sixteen RGB color
values, and a collection of arbitrary RGB colors to be mapped to their closest color in the target set. For our purposes, an RGB color is defined as an ordered triple (R,G,B) where each value of the triple is an integer from 0 to 255. The distance between
two colors is defined as the Euclidean distance between two three-dimensional points. That is, given two colors (R1,G1,B1) and (R2,G2,B2), their distance D is given by the equation

Input

The input is a list of RGB colors, one color per line, specified as three integers from 0 to 255 delimited by a single space. The first sixteen colors form the target set of colors to which the remaining colors will be mapped.
The input is terminated by a line containing three -1 values.

Output

For each color to be mapped, output the color and its nearest color from the target set.



If there are more than one color with the same smallest distance, please output the color given first in the color set.

二、题解

        这道题的核心部分就是算D的最小值,然后输出对应的RGB值。有点棘手的部分就是输入的字符串与数组之间的转换,不过总体没有难度。

三、java代码

import java.util.Scanner; 

public class Main {
public static int sqrtD(int a[],int b[]){
int x,y,z,d;
x=(int) Math.pow(a[0]-b[0], 2);
y=(int) Math.pow(a[1]-b[1], 2);
z=(int) Math.pow(a[2]-b[2], 2);
d=(int) Math.sqrt(x+y+z);
return d;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String []a=new String[3];
int []a1=new int[3];
String []b=new String[3];
int []b1=new int[3];
String[] s=new String[100];
String[] s2=new String[50];
int i=1,j=1,k,l,m;
s[0]=cin.nextLine();
while(!(s[i]=cin.nextLine()).equals(s[0])){
i++;
}
i--;
s2[0]=s[0];
while(!(s2[j]=cin.nextLine().trim()).equals("-1 -1 -1")){
j++;
}
j--;
int d;
String[] temp = null;
String ss;
for(k=0;k<=j;k++){
int min=Integer.MAX_VALUE;
a=s2[k].split(" ");
for(l=0;l<=i;l++){
b=s[l].split(" ");
for(m=0;m<3;m++){
a1[m]=Integer.parseInt(a[m]);
b1[m]=Integer.parseInt(b[m]);
}
d=sqrtD(a1,b1);
if(d<min){
min=d;
temp=b;
}
}
ss="("+a[0]+","+a[1]+","+a[2]+")"+" maps to "+"("+temp[0]+","+temp[1]+","+temp[2]+")";
System.out.println(ss);
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. 如何搭建 node,react 开发环境
  2. java线程的简单实现及方法
  3. js基础细节
  4. 为YAESU FT-817ND 增加频谱功能
  5. SQL Server 诊断查询-(3)
  6. 总结下遇到的C#新语法
  7. ajax处理回调函数,用ajax向后台发送数据
  8. 【二叉树-&gt;链表】二叉树结构转双向线性链表结构(先序遍历)
  9. JAVA的三大特征 封装继承多态- 简单总结
  10. class-决策树Decision Tree
  11. [ Java面试题 ]框架篇二
  12. P1101 单词方阵 (单词方阵)
  13. setting设置
  14. [颜色知识] 潘通色卡、CMYK、RGB、 ARGB...
  15. Spring依赖
  16. hdu 2838 Cow Sorting (树状数组+逆序对)
  17. Oracle:&quot;ORA-00942: 表或视图不存在&quot;
  18. if条件判断
  19. 我的nodejs 快速入门
  20. 银行卡验证API

热门文章

  1. ural 1303 Minimal Coverage【贪心】
  2. 【python】-- 列表
  3. Python菜鸟之路:Python基础(三)
  4. 【译】常见 Java 异常解释(恶搞版)
  5. Linux安装Nignx基于域名的多虚拟主机实战
  6. android 中使用svg
  7. openstack 官方镜像qcow2 下载和修改密码
  8. Android Screen Orientation
  9. 显示HTML的版权符号
  10. matlab常用的一些程序和功能