题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1046
思路:读懂题目就会做。。。前16行是一个给你判断的集合,后面才是要计算的具体数据,拿下面和上面16行依次计算,输出D最小的RGB就行。
- import java.util.Scanner;
- public class Main {
- int[][] rgb = new int[16][3];
- int r;
- int g;
- int b;
- int map;
- double d;
- double t;
- static final double D = Math.sqrt(255 * 255 * 3);
- public Main() {
- Scanner scan = new Scanner(System.in);
- for (int i = 0; i < 16; i++) {
- for (int j = 0; j < 3; j++) {
- rgb[i][j] = scan.nextInt();
- }
- }
- r = scan.nextInt();
- g = scan.nextInt();
- b = scan.nextInt();
- while (r != -1 && g != -1 && b != -1) {
- d = D;
- for (int i = 0; i < 16; i++) {
- t = Math.sqrt(Math.pow(rgb[i][0] - r, 2)
- + Math.pow(rgb[i][1] - g, 2)
- + Math.pow(rgb[i][2] - b, 2));
- if (t < d) {
- map = i;
- d = t;
- }
- }
- System.out.printf("(%d,%d,%d) maps to (%d,%d,%d)/n", r, g, b,
- rgb[map][0], rgb[map][1], rgb[map][2]);
- r = scan.nextInt();
- g = scan.nextInt();
- b = scan.nextInt();
- }
- }
- public static void main(String[] args) {
- new Main();
- }
- }