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
![](http://www.java3z.com/cwbwebhome/article/article17/img1/color.gif)
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代码
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);
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。