本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!