POJ 1046 Color Me Less 最详细的解题报告

题目来源:POJ 1046 Color Me Less

题目大意:每一个颜色由R、G、B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ Math.pow((left.green - right.green), 2)+ Math.pow((left.blue - right.blue), 2)) 表示两个不同颜色的之间的距离(以left和right为例,left和right分别为两种不同的颜色),现给出16组目标颜色,剩下的为待匹配的颜色,求出剩下的颜色与目标颜色中哪个最匹配,即D最小。

 

解题思路:直接枚举法,简单直接!

具体算法(java版,可以直接AC)

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     static double distance(Color left, Color right) {
 6         return Math.sqrt(Math.pow((left.red - right.red), 2)
 7                 + Math.pow((left.green - right.green), 2)
 8                 + Math.pow((left.blue - right.blue), 2));
 9     }
10 
11     static Color getColor(Scanner input) {
12         return new Color(input.nextInt(), input.nextInt(), input.nextInt());
13     }
14 
15     public static void main(String[] args) {
16         Scanner input = new Scanner(System.in);
17         int targetCount = 16;
18         Color[] targets = new Color[targetCount];
19         for (int i = 0; i < targetCount; i++) {
20             targets[i] = getColor(input);
21         }
22         while (true) {
23             Color object = getColor(input);
24             if (object.isEnd()) {
25                 return;
26             }
27             double min = Double.MAX_VALUE;
28             int minIndex = 0;
29             for (int i = 0; i < targetCount; i++) {
30                 double dist = distance(targets[i], object);
31                 if (dist < min) {
32                     min = dist;
33                     minIndex = i;
34                 }
35             }
36             System.out.println(String.format("%s maps to %s", object.toString(),
37                     targets[minIndex].toString()));
38         }
39     }
40 }
41 
42 class Color {
43     public int red;
44     public int green;
45     public int blue;
46 
47     public Color(int red, int green, int blue) {
48         this.red = red;
49         this.green = green;
50         this.blue = blue;
51     }
52 
53     public boolean isEnd() {
54         return this.red == -1 && this.green == -1 && this.blue == -1;
55     }
56 
57     public String toString() {
58         return String.format("(%d,%d,%d)", this.red, this.green, this.blue);
59     }
60 }

 

转载于:https://www.cnblogs.com/pinxiong/p/4001236.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值