1. initialize result[] to 0 and read in all trees
2. for each tree i, find the minimum distance amongst all other trees and store it in minDistance[i]
3. if minDistance[i] < 10, add 1 to the corresponding bin, ie, ++result[(int) minDistance[i]]
4. print out the result array
Did I miss something or misunderstand anything? Would anyone please provide a set of input/output for my testing - I can only find one in the forum and my program got it right. Here is my code:
- Code: Select all
#include <cstdio>
#include <cmath>
#include <climits>
using namespace std;
#ifndef DBL_MAX
const double DBL_MAX = 1.7976931348623158e+308;
#endif
struct Point
{
double x, y, z;
};
Point tree[5000];
int treeNum = 0;
int result[10] = {0};
double minDistance[5000];
int main()
{
for (int i = 0; i < 5000; ++i) {
minDistance[i] = DBL_MAX;
}
while (true) {
Point & p = tree[treeNum];
scanf("%lf %lf %lf", &p.x, &p.y, &p.z);
if (p.x == 0 && p.y == 0 && p.z == 0) break;
++treeNum;
}
for (int i = 0; i < treeNum; ++i) {
for (int j = 0; j < treeNum; ++j) {
if (i == j) continue;
double xDist = tree[j].x - tree[i].x;
double yDist = tree[j].y - tree[i].y;
double zDist = tree[j].z - tree[i].z;
double distSquare = xDist * xDist + yDist * yDist + zDist * zDist;
double dist = sqrt(distSquare);
if (dist < minDistance[i]) {
minDistance[i] = dist;
}
}
if (minDistance[i] < 10) {
++result[(int) minDistance[i]];
}
}
for (int i = 0; i < 10; ++i) {
printf("%4d", result[i]);
}
putchar('\n');
}
