欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

uva 152 - Tree's a Crowd

程序员文章站 2024-03-21 17:19:04
...

题目地址: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;
}