POJ1046

来源:互联网 发布:遥知不是雪为有暗香来 编辑:程序博客网 时间:2024/05/21 19:42

 题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1046

 思路:读懂题目就会做。。。前16行是一个给你判断的集合,后面才是要计算的具体数据,拿下面和上面16行依次计算,输出D最小的RGB就行。

 

  1. import java.util.Scanner;
  2. public class Main {
  3.     int[][] rgb = new int[16][3];
  4.     int r;
  5.     int g;
  6.     int b;
  7.     int map;
  8.     double d;
  9.     double t;
  10.     static final double D = Math.sqrt(255 * 255 * 3);
  11.     public Main() {
  12.         Scanner scan = new Scanner(System.in);
  13.         for (int i = 0; i < 16; i++) {
  14.             for (int j = 0; j < 3; j++) {
  15.                 rgb[i][j] = scan.nextInt();
  16.             }
  17.         }
  18.         r = scan.nextInt();
  19.         g = scan.nextInt();
  20.         b = scan.nextInt();
  21.         while (r != -1 && g != -1 && b != -1) {
  22.             d = D;
  23.             for (int i = 0; i < 16; i++) {
  24.                 t = Math.sqrt(Math.pow(rgb[i][0] - r, 2)
  25.                         + Math.pow(rgb[i][1] - g, 2)
  26.                         + Math.pow(rgb[i][2] - b, 2));
  27.                 if (t < d) {
  28.                     map = i;
  29.                     d = t;
  30.                 }
  31.             }
  32.             System.out.printf("(%d,%d,%d) maps to (%d,%d,%d)/n", r, g, b,
  33.                     rgb[map][0], rgb[map][1], rgb[map][2]);
  34.             r = scan.nextInt();
  35.             g = scan.nextInt();
  36.             b = scan.nextInt();
  37.         }
  38.     }
  39.     public static void main(String[] args) {
  40.         new Main();
  41.     }
  42. }
原创粉丝点击