Tree's a Crowd
Tree's a Crowd |
Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new classification system for trees. This is a complicated system involving a series of measurements which are then combined to produce three numbers (in the range [0, 255]) for any given tree. Thus each tree can be thought of as occupying a point in a 3-dimensional space. Because of the nature of the process, measurements for a large sample of trees are likely to be spread fairly uniformly throughout the whole of the available space. However Dr Larch is convinced that there are relationships to be found between close neighbours in this space. To test this hypothesis, he needs a histogram of the numbers of trees that have closest neighbours that lie within certain distance ranges.
Write a program that will read in the parameters of up to 5000 trees and determine how many of them have closest neighbours that are less than 1 unit away, how many with closest neighbours 1 or more but less than 2 units away, and so on up to those with closest neighbours 9 or more but less than 10 units away. Thus if is the distance between the i'th point and its nearest neighbour(s) and , with j and k integers and k = j+1, then this point (tree) will contribute 1 to the j'th bin in the histogram (counting from zero). For example, if there were only two points 1.414 units apart, then the histogram would be 0, 2, 0, 0, 0, 0, 0, 0, 0, 0.
Input and Output
Input will consist of a series of lines, each line consisting of 3 numbers in the range [0, 255]. The file will be terminated by a line consisting of three zeroes.
Output will consist of a single line containing the 10 numbers representing the desired counts, each number right justified in a field of width 4.
Sample input
10 10 0 10 10 0 10 10 1 10 10 3 10 10 6 10 10 10 10 10 15 10 10 21 10 10 28 10 10 36 10 10 45 0 0 0
Sample output
2 1 1 1 1 1 1 1 1 1
题意
在一个三维空间中,有不多于5000个点。请编写程序,求出其中有多少个点与离它最近的点之间的距离小于1个单位长度,有多少个点与离它最近的点之间的距离大于等于1个单位长度但小于2个单位长度,以此类推,求出有多少个点与离它最近的点之间的距离大于等于9个单位长度但小于10个单位长度。所以,如果di是第i个点和离它最近的点之间的距离,且j<=di<k,j,k均为整数,且k = j+1,那这个点就会使频率统计表中的j位置加1。举例说,假如只有两个相距1.414单位长度的点,频率统计表就会是0, 2, 0, 0, 0, 0, 0, 0, 0, 0。 (给出树木的三维整数坐标,计算每棵树到其他树木的最短距离,如果小于10则向下取整并加入0-10数组)
分析
求某点到其他所有点的距离,找到最近的距离,统计最近的距离在0~10之间的点的个数,注意每次最后距离的比较都是取整的。按要求输出就行。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define INF 0xffff
struct Point
{
double x, y, z;
}PT[5050];
int dis(Point a, Point b)
{
return (int)sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
int main()
{
int n = 0, num[11];
double x, y, z;
while(scanf("%lf%lf%lf", &x, &y, &z))
{
if(x==0 && y==0 && z==0) break;
PT[n].x = x, PT[n].y = y, PT[n].z = z;
n++;
}
memset(num, 0, sizeof(num));
for(int i = 0; i < n; i++)
{
int mind = 10;
for(int j = 0; j < n; j++)
{
if(i==j) continue;
int d = dis(PT[i], PT[j]);
if(mind > d) mind = d;
}
if(mind < 10) num[mind]++;
}
for(int i = 0; i < 10; i++)
printf("%4d", num[i]);
printf("\n");
return 0;
}