POJ 1046 Color Me Less(水题)
来源:互联网 发布:南京plc编程培训机构 编辑:程序博客网 时间:2024/05/16 09:09
题意:先输入16种色彩,称之为集合A,然后再输入N种色彩,这些色彩为集合B,遇到色彩为输入为-1,-1,-1时,结束输入。然后,要求从B到A有个映射,这个映射的距离最短,即为下面的公式的值最小
然后,按照标准格式输出。
分析:思路就是想用一个数组将B集合到A集合的映射的所有距离全部存起来,然后按照从小到大的顺序排序。当然,要先用另外一个数组保存一下原始的数组,用于后续的判断。注意题目中的一句话Ifthere are more than one color with the same smallest distance,please output the color given first in the colorset.如果有多个同样的距离,按照顺序输出第一个就好。所以在输出的过程中要注意循环的终止。
做这种题目思路多是如此。
第一次就是忘了写个break,WA了一次
贴下代码:(204k,0ms)
C++语言:
#include<iostream>
#include<cmath>
#include<algorithm>
usingnamespacestd;
intmain()
{
int a[16][3];
int b[100][3]={0};
double d[100][16]; //两个颜色的距离
double original_d[100][16];
int distance=0;
int max;
int i,j,k;
int temp;
for(i =0;i < 16 ;i ++)
{
for(j =0;j < 3;j++)
{
cin>>a[i][j];
}
}
for(i =0;i<100;i++)
{
temp= 0;
for(j =0;j<3;j++)
{
cin>>b[i][j];
if(b[i][j]== -1)
temp ++;
}
if(temp ==3)
{
max= i;
break;
}
}
for(i =0 ;i < max;i++)
for(j =0;j < 16; j ++) //j是16个数据中的第j+1个数据
{
distance= 0;
for(k= 0;k < 3;k ++)
distance += (b[i][k]-a[j][k])*(b[i][k]-a[j][k]);
d[i][j]= sqrt((double)distance);
original_d[i][j]= d[i][j];
}
for(i =0;i < max ; i++)
{
sort(d[i],d[i]+ 16);
}
for(i =0;i < max;i++)
for(k= 0;k < 16; k ++)
{
if(original_d[i][k]== d[i][0])
// cout<<k<<endl;
{
cout<<"("<<b[i][0]<<","<<b[i][1]<<","<<b[i][2]<<")"<<"maps to "<<"("<<a[k][0]<<","<<a[k][1]<<","<<a[k][2]<<")"<<endl;
break;
}
#include<cmath>
#include<algorithm>
usingnamespacestd;
intmain()
{