152 - Tree's a Crowd

题目:152 - Tree's a Crowd


题目大意:找两颗树之间最近的距离,判断有多少的距离在0 - 10 之间;

解题思路:遍历,两两比较,注意要判断距离是否小于10 , 小于才加加。

#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;

const int N = 5005;
int t, s[15];
int n1, n2;

struct TREE{

	int x1, y1, z1;
} tree[N];


int caculate (TREE &a, TREE &b) {
	
		double sum = (a.x1 - b.x1) * (a.x1 - b.x1) + (a.y1 - b.y1) * (a.y1 - b.y1) + (a.z1 - b.z1) * (a.z1 - b.z1);
		return (int)sqrt(sum);
}

int main() {

	t = 0;
	int i, j;
	memset(s, 0,  sizeof(s));
	while(scanf("%d %d %d", &tree[t].x1, &tree[t].y1, &tree[t].z1)) {	

		if((tree[t].x1 + tree[t].y1 + tree[t].z1) == 0 ) {
				break;
		}
		t++;	
	}

	int d, min ;
	for( i = 0; i < t; i++) {
		min = 10;
		for (j = 0; j < t; j++) {
		
			if( i != j) {

				d = caculate(tree[i], tree[j]);	
			if(min > d)

				min = d;
			}
		}
		if(min < 10)
		s[min]++;
	}

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


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