uva 152 - Tree's a Crowd

题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=88

 

C++ AC。理解题意,找出每棵树最近邻树的距离,进行统计。

/*
 * uva152.cpp
 *
 *  Created on: 2013-4-12
 *      Author: kevinjiang
 */
#include<cstdio>
#include<cmath>

const int MAX = 5005;

struct tree {
	double x;
	double y;
	double z;
};

struct tree trees[MAX];
int result[10];

double distance(struct tree a, struct tree b) {
	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));
}

int main() {
	int n = 0;
	for (int i = 0;; i++) {
		scanf("%lf%lf%lf", &trees[i].x, &trees[i].y, &trees[i].z);
		if (trees[i].x == 0 && trees[i].y == 0 && trees[i].z == 0) {
			n = i;
			break;
		}
	}

	for (int i = 0; i < n; i++) {
		double min = sqrt(3*255*255+1);

		for(int j=0;j<n;j++)
		{
			if(i!=j)
			{
				double dis=distance(trees[i],trees[j]);
				if(dis<min)
					min=dis;
			}

		}
		int index=(int)min;
		if(index<10)
			result[index]++;

	}

	for(int i=0;i<10;i++)
		printf("%4d",result[i]);
	printf("\n");

	return 0;
}

 

你可能感兴趣的:(uva 152 - Tree's a Crowd)