Tree's a Crowd
Tree's a Crowd |
Dr William Larch, noted plant psychologist and inventor of the phrase``Think like a tree--Think Fig'' has invented a new classificationsystem for trees. This is a complicated system involving a series ofmeasurements which are then combined to produce three numbers (in therange [0, 255]) for any given tree. Thus each tree can be thought ofas occupying a point in a 3-dimensional space. Because of the natureof the process, measurements for a large sample of trees are likely tobe spread fairly uniformly throughout the whole of the availablespace. However Dr Larch is convinced that there are relationships tobe found between close neighbours in this space. To test thishypothesis, he needs a histogram of the numbers of trees that haveclosest neighbours that lie within certain distance ranges.
Write a program that will read in the parameters of up to 5000 treesand determine how many of them have closest neighbours that are lessthan 1 unit away, how many with closest neighbours 1 or more but lessthan 2 units away, and so on up to those with closest neighbours 9 ormore but less than 10 units away. Thus if is thedistance between the i'th point and its nearest neighbour(s) and , with j and k integers and k = j+1, thenthis point (tree) will contribute 1 to the j'th bin in the histogram(counting from zero). For example, if there were only two points 1.414units apart, then the histogram would be 0, 2, 0, 0, 0, 0, 0, 0, 0, 0.
Input and Output
Input will consist of aseries of lines, each line consisting of 3 numbers in the range [0,255]. The file will be terminated by a line consisting of threezeroes.
Output will consist of a single line containing the 10 numbersrepresenting the desired counts, each number right justified in afield of width 4.
Sample input
10 10 0 10 10 0 10 10 1 10 10 3 10 10 6 10 10 10 10 10 15 10 10 21 10 10 28 10 10 36 10 10 45 0 0 0
Sample output
2 1 1 1 1 1 1 1 1 1 题解: 给出三维空间的坐标, 不管题目怎么讲, 它都是点的概念 直到给出三个0, 作为结束标志. 接着对每一个点, 求该点到其他所有剩余点的距离, 把最短的哪个距离存下来~ 没了, 水题~ AC代码:#include<stdio.h> #include<string.h> #include<math.h> int tree[5050][3]; int ans[5050]; int Min; int main() { int num = 0; while(scanf("%d %d %d", &tree[num][0], &tree[num][1], &tree[num][2]) != EOF) { if(tree[num][0] == 0 && tree[num][1] == 0 && tree[num][2] == 0) break; else num++; } memset(ans, 0, sizeof(ans)); double d; int Min; for(int i = 0; i < num; i++) { Min = 999999999; for(int j = 0; j < num; j++) { if(i != j) { d = sqrt((tree[i][0] - tree[j][0]) * (tree[i][0] - tree[j][0]) + (tree[i][1] - tree[j][1]) * (tree[i][1] - tree[j][1]) + (tree[i][2] - tree[j][2]) * (tree[i][2] - tree[j][2])); if(d < Min) Min = d; } } int res = (Min + 0.5); if(res < 10) ans[res]++; } for(int i = 0; i < 10; i++) printf("%4d", ans[i]); printf("\n"); return 0; }