Problem 1046 Color Me Less

SourceCode(Java)

import java.util.*;

class Main
{
	public static int dist(int[] pal,int[] col)
	{
		return (pal[0]-col[0])*(pal[0]-col[0])
			+(pal[1]-col[1])*(pal[1]-col[1])
			+(pal[2]-col[2])*(pal[2]-col[2]);
	}
	
	private static int[][] pallet = new int[16][3];
	
	public static void main(String[] args)
	{ 
		Scanner lex = new Scanner(System.in);
		for(int i=0;i<16;i++)
		{
			for(int j=0;j<3;j++)
			{
				pallet[i][j] = lex.nextInt(); 
			}
		}
		while(true)
		{
			int[] color = new int[3];
			int d = 1000000;
			int cNum=0;
			for(int i=0;i<3;i++) color[i]=lex.nextInt();
			
			if(color[0]==-1)break;
			
			for(int i=0;i<16;i++)
			{
				int tmp;
				if((tmp = dist(pallet[i],color)) < d)
				{
					d = tmp;
					cNum = i;
 				}
			}
			System.out.println(
				"("+color[0]+","+color[1]+","+color[2]+") maps to  ("+pallet[cNum][0]+","+pallet[cNum][1]+","+pallet[cNum][2]+")"); 
		}
	}
}
最終更新:2006年09月11日 17:57