UVa 152 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 iftex2html_wrap_inline26 is the distance between the i'th point and its nearest neighbour(s) and tex2html_wrap_inline28 , 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

题目的意思就是在给一些三维坐标系中的点,对每个点求到它距离最短的点的距离,最后统计这样的距离在0~1,1~2,……,9~10的点的个数

由于点数不超过5000,可以直接将所有的两点之间距离都求出来保存在一个二维数组中,再统计最短,不用任何算法也不会超时。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 
 5 using namespace std;
 6 
 7 typedef struct
 8 {
 9     double x,y,z;
10 } POINT;
11 
12 double dis[5050][5050];
13 
14 double Distance(POINT a,POINT b)
15 {
16     return 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) );
17 }
18 
19 int main()
20 {
21     POINT point[5050];
22     int ans[11];
23     int total=0;
24     double a,b,c;
25     while(scanf("%lf %lf %lf",&a,&b,&c))
26     {
27         if(!(a||b||c))
28             break;
29         point[total].x=a;
30         point[total].y=b;
31         point[total].z=c;
32         total++;
33     }
34 
35     for(int i=0;i<total;i++)
36         for(int j=0;j<total;j++)
37             dis[i][j]=Distance(point[i],point[j]);
38 
39     for(int i=0;i<=10;i++)
40         ans[i]=0;
41 
42     for(int i=0;i<total;i++)
43     {
44         double temp=200000;
45         for(int j=0;j<total;j++)
46             if(i!=j&&dis[i][j]<temp)
47                 temp=dis[i][j];
48         if(temp<1)
49             ans[0]++;
50         else if(temp<2)
51             ans[1]++;
52         else if(temp<3)
53             ans[2]++;
54         else if(temp<4)
55             ans[3]++;
56         else if(temp<5)
57             ans[4]++;
58         else if(temp<6)
59             ans[5]++;
60         else if(temp<7)
61             ans[6]++;
62         else if(temp<8)
63             ans[7]++;
64         else if(temp<9)
65             ans[8]++;
66         else if(temp<10)
67             ans[9]++;
68     }
69 
70     for(int i=0;i<10;i++)
71         printf("%4d",ans[i]);
72     printf("
");
73 
74     return 0;
75 }
[C++]
原文地址:https://www.cnblogs.com/lzj-0218/p/3504377.html