一、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 sixteen RGB color
values, and a collection of arbitrary RGB colors to be mapped to their closest color in the target set. For our purposes, an RGB color is defined as an ordered triple (R,G,B) where each value of the triple is an integer from 0 to 255. The distance between
two colors is defined as the Euclidean distance between two three-dimensional points. That is, given two colors (R1,G1,B1) and (R2,G2,B2), their distance D is given by the equation

Input

The input is a list of RGB colors, one color per line, specified as three integers from 0 to 255 delimited by a single space. The first sixteen colors form the target set of colors to which the remaining colors will be mapped.
The input is terminated by a line containing three -1 values.

Output

For each color to be mapped, output the color and its nearest color from the target set.



If there are more than one color with the same smallest distance, please output the color given first in the color set.

二、题解

        这道题的核心部分就是算D的最小值,然后输出对应的RGB值。有点棘手的部分就是输入的字符串与数组之间的转换,不过总体没有难度。

三、java代码

import java.util.Scanner; 

public class Main {
public static int sqrtD(int a[],int b[]){
int x,y,z,d;
x=(int) Math.pow(a[0]-b[0], 2);
y=(int) Math.pow(a[1]-b[1], 2);
z=(int) Math.pow(a[2]-b[2], 2);
d=(int) Math.sqrt(x+y+z);
return d;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String []a=new String[3];
int []a1=new int[3];
String []b=new String[3];
int []b1=new int[3];
String[] s=new String[100];
String[] s2=new String[50];
int i=1,j=1,k,l,m;
s[0]=cin.nextLine();
while(!(s[i]=cin.nextLine()).equals(s[0])){
i++;
}
i--;
s2[0]=s[0];
while(!(s2[j]=cin.nextLine().trim()).equals("-1 -1 -1")){
j++;
}
j--;
int d;
String[] temp = null;
String ss;
for(k=0;k<=j;k++){
int min=Integer.MAX_VALUE;
a=s2[k].split(" ");
for(l=0;l<=i;l++){
b=s[l].split(" ");
for(m=0;m<3;m++){
a1[m]=Integer.parseInt(a[m]);
b1[m]=Integer.parseInt(b[m]);
}
d=sqrtD(a1,b1);
if(d<min){
min=d;
temp=b;
}
}
ss="("+a[0]+","+a[1]+","+a[2]+")"+" maps to "+"("+temp[0]+","+temp[1]+","+temp[2]+")";
System.out.println(ss);
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1046 Color Me Less(浅水)的更多相关文章

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

    题目来源:POJ 1046 Color Me Less 题目大意:每一个颜色由R.G.B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ M ...

  2. poj 1046 ——Color Me Less

    提交地址:http://poj.org/problem?id=1046 Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. poj 1046 Color Me Less

    Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33007   Accepted: 16050 D ...

  4. [ACM] POJ 1046 Color Me Less

    Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30146   Accepted: 14634 D ...

  5. 组合数学 - 波利亚定理 --- poj : 2154 Color

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7873   Accepted: 2565 Description ...

  6. [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description ...

  7. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  8. poj 2777Count Color

    http://poj.org/problem?id=2777 注意:a可能比b大 #include <cstdio> #include <cstring> #include & ...

  9. poj 2154 Color——带优化的置换

    题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...

随机推荐

  1. 九度OJ 1354:和为S的连续正数序列 (整除)

    时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:2028 解决:630 题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不 ...

  2. Arrays数组工具类中存在的坑!

    以下是一个坑: 看到使用asList时候,可以返回一个集合,当然集合拥有CRUD的特性: 代码中使用 了add和remove时候均报错:Exception in thread "main&q ...

  3. MySQL 练习 答案

    表关系 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号和平均成绩: 4.查询所有同学的学号.姓名.选课数.总成绩: ...

  4. hibernate多对多关系配置

    一.创建用户,角色实体类. 一名用户可以有多个角色.一个角色可以对于多名用户. 用户实体类 public class User { private int uId; private String uN ...

  5. Struts2-Value Stack浅析

    http://my.oschina.net/mlongbo/blog/88250 Value Stack的作用: 1.       可以作为一个数据中转站 2.       用于在前台-后台之间传递数 ...

  6. 创建美国地区的appleId

      参考: https://zhuanlan.zhihu.com/p/36574047 美国人身份信息生成: https://www.fakeaddressgenerator.com/Index/in ...

  7. 【leetcode刷题笔记】Single Number

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  8. 剑指offer——不能被继承的类

    方法一:通过将类的构造函数和析构函数声明成private来防止子类继承.声明静态的方法来构造和析构类的对象. 但是用起来不是很方便.只能得到在堆上的实例,而不能得到在栈上的实例. 方法二:构造辅助类C ...

  9. poj 1328 Radar Installatio【贪心】

    题目地址:http://poj.org/problem?id=1328 Sample Input 3 2 1 2 -3 1 2 1 1 2 0 2 0 0 Sample Output Case 1: ...

  10. HDU OJ 2159 FATE

    #include <stdio.h> #include <string.h> ][] ; ]; //»ñµÃ¾­Ñé ]; //»¨·ÑµÄÈÌÄÍ¶È int main() ...