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 }
时间: 2024-08-01 09:19:21

POJ 1046 Color Me Less 最详细的解题报告的相关文章

POJ 1050 To the Max 最详细的解题报告

题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵state,state[j][k]用来存放矩阵的某行中第j到k个元素的最大值: 2.对于行如何处理呢?我们可以将第一行中的N个元素的所有组合的最大值存放在state中,如果有哪个值小于0,清零,因为它没有做任何贡献:定计算第二行时与第一行的值(全部大于等于0)进行累加,这样就完成了第一行与第二行的累

POJ 1063 Flip and Shift 最详细的解题报告

题目来源:Flip and Shift 题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起.你可以有以下两种操作: 1.顺时针旋转所有的盘子 2.顺时针旋转3个盘子 解题思路:第一种操作表明你可以对任意一个盘子执行第二种操作,所以重点在于第二种操作.仔细分析一下会发现,第二种操作其实就是将某一个盘子当前的index加2,所以我们可以先统计一下黑色盘子的个数count,然后用一个数组将所有的盘子存起来,使数组中0-count所存放的是黑色盘子(通过下标加2

[ACM] POJ 1046 Color Me Less

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30146   Accepted: 14634 Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just

poj 1046 Color Me Less

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33007   Accepted: 16050 Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just

POJ 1046 Color Me Less解题报告

Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of

POJ 1057 File Mapping 最详细的解题报告

题目来源:POJ 1057 File Mapping 题目大意:像我的电脑那样显示文件夹和文件信息,其中在同一级目录内,文件夹排在文件的前面并且文件夹的顺序不变,同一级目录中文件按字母序排列.文件以‘f’开头,文件夹以‘d’开头,‘*’表示一个case的结束,‘#’表示所有输入内容结束. 解题思路:递归创建,最后递归输出.算法比较简单,我就不细说了,具体看代码: 具体算法(java版,可以直接运行) 1 import java.util.*; 2 3 public class Main { 4

POJ 1047 Round and Round We Go 最详细的解题报告

题目链接:Round and Round We Go 解题思路:用程序实现一个乘法功能,将给定的字符串依次做旋转,然后进行比较.由于题目比较简单,所以不做过多的详解. 具体算法(java版,可以直接AC) 1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner scanner = new Scanner(System.in); 7 Stri

POJ 1328(模拟&amp;贪心_A题)解题报告

题目链接:http://poj.org/problem?id=1328 -------------------------------------------------------- 题意:在平面直角坐标系上,给出多个点.寻找能覆盖的圆的最小个数. 思路:乍一看容易陷入寻找圆的圆心的思路中,然而确定圆心不是一个很容易的事情,其确定方法与其他各点都有一定关系.所以这种思路不可取.然后先从最简单的情况下考虑,如果纵坐标大于圆的半径,那么无论何种方式都不能满足题意,所以输出"-1".由此获

POJ 2260(模拟&amp;贪心_B题)解题报告

题目链接:http://poj.org/problem?id=2260 -------------------------------------------------------- 题意:一种校验码,查出一位的比特差错. 思路:校验码的原理与思路很明确,计算出每行每列的和,找出该和为奇数的点,该点所对应的x,y坐标即为所求.超过两位输出"Corrupt". 代码: #include<cstdio> #include<iostream> using names