UVa 152 - Tree's a Crowd

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

计算每个树到其他树的最近距离,如果这个距离小于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  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).

因此如果最近距离大于等于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;
}





你可能感兴趣的:(ACM,uva,152)