题目简化后为:输入N个3维坐标...N要大于16....然后16以后的坐标同前16个坐标比较..找出与之最近的一个坐标并输出
例子:Example
Input
0 0 0
255 255 255
0 0 1
1 1 1
128 0 0
0 128 0
128 128 0
0 0 128
126 168 9
35 86 34
133 41 193
128 0 128
0 128 128
128 128 128
255 0 0
0 1 0
0 0 0
255 255 255
253 254 255
77 79 134
81 218 0
-1 -1 -1
Output
(0,0,0) maps to (0,0,0)
(255,255,255) maps to (255,255,255)
(253,254,255) maps to (255,255,255)
(77,79,134) maps to (128,128,128)
(81,218,0) maps to (126,168,9)
#include<iostream.h>
#include<math.h>
struct colour
{int x;
int y;
int z;
}color[30];
void compare(int num)
{double d;
double min;
int temp=1;
int i,j;
for(i=17;i<=num;i++)
{min=sqrt(pow(color[i].x-color[1].x,2)+pow(color[i].y-color[1].y,2)+pow(color[i].z-color[1].z,2));
for(j=1;j<=16;j++)
{d=sqrt(pow(color[i].x-color[j].x,2)+pow(color[i].y-color[j].y,2)+pow(color[i].z-color[j].z,2));
if(d<min)
{min=d;
temp=j;
}
}
cout<<"("<<color[i].x<<","<<color[i].y<<","<<color[i].z<<")"
<<" maps to "<<"("<<color[temp].x<<","<<color[temp].y<<","<<color[temp].z<<")"<<endl;
}
}
int main()
{
int m,n,l;
int i;
int counter=0;
for(i=1;i<30;i++)
{cin>>m>>n>>l;
if(m!=-1&&n!=-1&&l!=-1)
{
counter++;
color[i].x=m;
color[i].y=n;
color[i].z=l;
}
else
break;
}
compare(counter);
return 0;
}
把例子中的输入进去答案是正确的..但是如果换个简单的
比方说前13个是1 1 1,14个 1 2 3,15个是1 0 2,16个是0 0 0,
16以后是
1 2 3
0 0 0
1 1 1
这样就不会是正确答案了!
麻烦大家帮我看看...3Q~