UVa 152 - Tree's a Crowd

本文介绍了一种算法,用于计算三维空间中树木之间的最近距离,并统计这些距离落在不同区间内的数量。通过遍历每棵树并计算其与其他树之间的距离,可以找出最短距离,并按1单位的距离区间进行分组计数。

简单题.不过我看的时候看不懂,之后参考了别人的解题报告才明白题目的意思

计算每个树到其他树的最近距离,如果这个距离小于10,保存.

最后输出每个距离的数量.

摘抄一些比较重要的句子.

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.

测定有多少最近的距离小于1, 多少大于1小于2, 等等直到最近的距离大于9小于10;

Thus if tex2html_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).

因此如果最近距离大于等于j小于k, j和k都是整数切相差1, 那么这个值将为j加一.


#include <stdio.h>
#include <math.h>
typedef struct point
{
    int x, y, z;
};
int main()
{
    //freopen("input.txt", "r", stdin);
    int cnt= 0;
    int num[10] = {0};
    struct point tree[5500];
    int i, j;
    for (i = 0;; i++)
    {
        scanf("%d%d%d", &tree[i].x, &tree[i].y, &tree[i].z);
        if (tree[i].x + tree[i].y + tree[i].z == 0)
        {
            break;
        }
        cnt = i + 1;
    }
    for (i = 0; i < cnt; i++)
    {
        int mindis = 1000;
        for (j = 0; j < cnt; j++)
        {
 
            if (j == i)
             continue;
            else
            {
                int c = (int)sqrt(pow(tree[i].x - tree[j].x, 2) + pow(tree[i].y - tree[j].y, 2) + pow(tree[i].z - tree[j].z, 2));
                if (c < mindis)
                    mindis = c;
            }
        }
        if (mindis < 10)
            num[mindis]++;
    }
    for (i = 0; i < 10; i++)
        printf("%4d", num[i]);
    printf("\n");
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值