POJ1046

问题描述###

给一组(16)个RGB颜色组合,叫做Target
然后再给你一组RGB,叫做Map
然后,对于Map中的每个RGB,求Target中离它最近的RGB


难点###

无~
直接贴代码吧


代码实现###

package poj; import java.util.ArrayList; import java.util.Scanner; public class Poj1046 { static int RGB = 3; public static void main(String[] args) { boolean flag = false; int TSS = 16; ArrayList targetSet = new ArrayList(); Scanner sc = new Scanner(System.in); /* target set */ for(int i = 0 ; i < TSS ; i ++){ String tmp = sc.nextLine(); String[] ss = tmp.split(" "); targetSet.add(new RGB(Integer.valueOf(ss[0]), Integer.valueOf(ss[1]), Integer.valueOf(ss[2]))); } /* next case */ String tmp = sc.nextLine(); String[] ss = tmp.split(" "); int b1 = Integer.valueOf(ss[0]); int b2 = Integer.valueOf(ss[1]); int b3 = Integer.valueOf(ss[2]); /* map set */ while(b1+b2+b3 != -3){ RGB mapRGB = new RGB(b1, b2, b3); int mapped = -1;; double min = Double.MAX_VALUE; for(int i = 0 ; i < targetSet.size() ; i ++){ double dist = mapRGB.distTo(targetSet.get(i)); if(dist < min){ min = dist; mapped = i; } } System.out.println(mapRGB.toString()+" maps to "+targetSet.get(mapped).toString()); tmp = sc.nextLine(); ss = tmp.split(" "); b1 = Integer.valueOf(ss[0]); b2 = Integer.valueOf(ss[1]); b3 = Integer.valueOf(ss[2]); } sc.close(); } public static class RGB { int R; int G; int B; public RGB(int R, int G, int B) { this.R = R; this.G = G; this.B = B; } public double distTo(RGB t){ double dist = 0.0; dist += Math.pow(this.R - t.R, 2); dist += Math.pow(this.G - t.G, 2); dist += Math.pow(this.B - t.B, 2); dist = Math.sqrt(dist); return dist; } public String toString(){ return "("+R+","+G+","+B+")"; } } }

你可能感兴趣的:(POJ1046)