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 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 terminated by a line containing three -1 values.
Output
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(浅水)的更多相关文章
- POJ 1046 Color Me Less 最详细的解题报告
题目来源:POJ 1046 Color Me Less 题目大意:每一个颜色由R.G.B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ M ...
- poj 1046 ——Color Me Less
提交地址:http://poj.org/problem?id=1046 Color Me Less Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1046 Color Me Less
Color Me Less Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33007 Accepted: 16050 D ...
- [ACM] POJ 1046 Color Me Less
Color Me Less Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30146 Accepted: 14634 D ...
- 组合数学 - 波利亚定理 --- poj : 2154 Color
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7873 Accepted: 2565 Description ...
- [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7630 Accepted: 2507 Description ...
- POJ 2054 Color a Tree
贪心.... Color a Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: ...
- poj 2777Count Color
http://poj.org/problem?id=2777 注意:a可能比b大 #include <cstdio> #include <cstring> #include & ...
- poj 2154 Color——带优化的置换
题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...
随机推荐
- iOS开发之获取系统相册ALAssetLibrary
注:当你选择看这篇博客时想必你的应用还支持iOS8一下系统,如果你的应用要求最低版本大于iOS8,建议使用PhotoKit框架,效率更高 ALAssetsLibrary包含,ALAssetsLibra ...
- MySQL合并多行
select id,group_concat(re_id order by re_id separator ",") as re_idfrom tablenamegroup by ...
- CSS 布局实例系列(二)如何通过 CSS 实现一个左边固定宽度、右边自适应的两列布局
最近在百度 IFE 训练营中看见的一道题目: 用两种不同的方法来实现一个两列布局,其中左侧部分宽度固定.右侧部分宽度随浏览器宽度的变化而自适应变化 个人总结出如下三种实现思路: 通过绝对定位实现 S ...
- react create app ,nginx服务器配置
server{ listen 80; server_name www.domain.com domain.com; location ~* \.js$ { root /home/hard/Projec ...
- ABAP内表数据做层次XML输出
*&---------------------------------------------------------------------**& Report Z_BARRY ...
- (转)Javascript模块化编程(三):require.js的用法
这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的 ...
- strstr使用
extern char strstr(char str1, const char *str2); 语法: strstr(str1,str2) str1: 被查找目标 string expression ...
- Hive2.0的新特性介绍
- 每天一个Linux命令(8)cat命令
cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的type命令. 注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容.因此,一般用more等命 ...
- dedecms常用标签
下面总结了58种常见的标签调用,包括关键描述调用.指定调用栏目.列表文章调用.频道栏目调用.当前栏目名称.栏目导航调用.模板路径调用.网站标题调用.友情链接调用.网站版权调用.网站备案调用.当前位置调 ...