POJ 1046 Color Me Less(浅水)

一、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);
       }
    } 
  } 


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

转载于:https://www.cnblogs.com/AndyDai/p/4734185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值