蛮水的题目,开始觉得好像直接比较大小不能过一样,结果证明是我想多了。
题意:将空间中的点按照到邻近点的最小距离分区间[0,1)~[9,10)
代码:
//#define LOCAL
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAXN 5000 + 10 // 最大树的数目
int calcDistance(int i, int j);// 参考了别人的想法,将计算的结果直接强转成int
struct TreePoint // 保存每个点的位置
{
int x;
int y;
int z;
}trees[MAXN];
int result[11]; // 保存最后的结果
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
int a, b, c;
int count, i, j;// 标记变量
// 数据初始化
memset(trees, 0, sizeof(trees));
memset(result, 0, sizeof(result));
// 算法输入
count = 0;
while(scanf("%d%d%d", &a, &b, &c) == 3 && a + b + c != 0)
{
trees[count].x = a;
trees[count].y = b;
trees[count].z = c;
count++;
}
for(i = count - 1; i >= 0; i--)
{
int mindistance = 500;
for(j = count - 1; j >= 0; j--)
{
if(i != j)
{
int distance = calcDistance(i, j);
// printf("distance=%d\n", distance);
if(distance < mindistance)
{
mindistance = distance;
}
}
}
//printf("mindistance=%d\n", mindistance);
if(mindistance < 10)
result[mindistance]++;
}
for(i = 0; i < 10; i++)
{
printf("%4d", result[i]);
}
printf("\n");
return 0;
}
int calcDistance(int i, int j)// 参考了别人的想法,将计算的结果直接强转成int
{
int x, y, z;
x = trees[i].x - trees[j].x;
// printf("x=%d\n", x);
y = trees[i].y - trees[j].y;
// printf("y=%d\n", y);
z = trees[i].z - trees[j].z;
// printf("z=%d\n", z);
return floor(sqrt((x * x + y * y + z * z)));
}