Uva 152 - Tree's a Crowd
Time limit: 3.000 seconds
Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like a tree--Think Fig'' has invented a new classification system for trees. This is a complicated system involving a series of measurements which are then combined to produce three numbers (in the range [0, 255]) for any given tree. Thus each tree can be thought of as occupying a point in a 3-dimensional space. Because of the nature of the process, measurements for a large sample of trees are likely to be spread fairly uniformly throughout the whole of the available space. However Dr Larch is convinced that there are relationships to be found between close neighbours in this space. To test this hypothesis, he needs a histogram of the numbers of trees that have closest neighbours that lie within certain distance ranges.
Write a program that will read in the parameters of up to 5000 trees and determine how many of them have closest neighbours that are less than 1 unit away, how many with closest neighbours 1 or more but less than 2 units away, and so on up to those with closest neighbours 9 or more but less than 10 units away. Thus if is the distance between the i'th point and its nearest neighbour(s) and
, with j and k integers and k = j+1, then this 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.414 units apart, then the histogram would be 0, 2, 0, 0, 0, 0, 0, 0, 0, 0.
Input and Output
Input will consist of a series of lines, each line consisting of 3 numbers in the range [0, 255]. The file will be terminated by a line consisting of three zeroes.
Output will consist of a single line containing the 10 numbers representing the desired counts, each number right justified in a field 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
#include<stdio.h> #include<string.h> #include<math.h> #define MAXN 195075 int path[5002][4]; int deal(int x, int y, int z, int a, int b, int c) { double _x = (double)x, _y = (double)y, _z = (double)z; double _a = (double)a, _b = (double)b, _c = (double)c; double dis; dis = sqrt((_x - _a)*(_x - _a) + (_y - _b)*(_y - _b) + (_z - _c)*(_z - _c)); return (int)floor(dis); } int main() { int i, j, n, x, y, z, count = 0, closedge[12], k, m; memset(path, 0, sizeof(path)); memset(closedge, 0, sizeof(closedge)); while(scanf("%d%d%d", &x, &y, &z) != EOF) { if(!x && !y && !z) { for(i=0; i<count; ++i) { k = MAXN; for(j=0; j<count; ++j) { if(i != j) { n = deal(path[i][0], path[i][1], path[i][2], path[j][0], path[j][1], path[j][2]); if(n < k) k = n; } } if(k < 10) closedge[k]++; } for(i=0; i<10; ++i) printf("%4d", closedge[i]); printf("\n"); } else { path[count][0] = x; path[count][1] = y; path[count++][2] = z; } } return 0; }
解题报告:
对于每棵树找出与它最近的那个树的距离,如果小于10(换成不大于实际距离的最大整数)则在统计该特定距离的区域标记加1, 最后是看这些树中两树距离为 0 - 9 的树各有多少棵, 简单的比较两数的大小的题

更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?