UVa 152 - Tree's a Crowd

本文探讨了由植物心理学家Dr. William Larch发明的一种新型树木分类系统,该系统通过一系列测量并组合产生三维空间中每个点的三个数值,从而为每棵树赋予一个独特位置。通过对大量样本树的数据进行分析,Larch博士提出了一个假设,即在三维空间中,相邻的树木之间存在某种联系。为了验证这一假设,文章提供了一个程序,用于读取最多5000棵树的参数,并确定它们与最近邻树之间的距离范围,进而生成距离分布直方图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这道题目主要是理解三个数是坐标然后求最短的距离;

#include <stdio.h>
#include <math.h>
#include <string.h>
double  d[10000000][3];
int main()
{
    int i,j,n,t,flag;
    double a,b,c,k;
    double s;
    int e[10];
    for(i=0;i<=9;i++)
    {
        e[i]=0;
    }
    i=0;
    while(scanf("%lf %lf %lf",&a,&b,&c)!=EOF)
    {
        if(a==0&&b==0&&c==0)
        {
            break;
        }
        d[i][0]=a;d[i][1]=b;d[i][2]=c;
        i+=1;
    }
    n=i;
    for(i=0;i<=n-1;i++)
    {
        flag=0;
        for(j=0;j<=n-1;j++)
        {
            if(j!=i)
            {
                s=(d[j][0]-d[i][0])*(d[j][0]-d[i][0])+(d[j][1]-d[i][1])*(d[j][1]-d[i][1])+(d[j][2]-d[i][2])*(d[j][2]-d[i][2]);
                s=sqrt(s);
                if(flag==0)
                {
                    k=s;
                    flag=1;
                }else
                {
                    if(s<k)
                    {
                        k=s;
                    }
                }
            }
        }
        t=(int)k;
        if(t<=9)
        {
            e[t]+=1;
        }
    }
    for(i=0;i<=9;i++)
    {
        printf("%4d",e[i]);
    }
    printf("\n");
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值