Uva--152 (检索,距离计算)
2014-06-03 00:03:22
题意&思路:计算距离&检索,不多说。
#include <cstdio> #include <cmath> #include <iostream> using namespace std; const int maxn = 5000; const double INF = 1000000; const double eps = 1e-8; struct Tree{ double x,y,z; }; Tree t[maxn + 5]; double Dis(double x1,double y1,double z1,double x2,double y2,double z2){ double x = x2 - x1; double y = y2 - y1; double z = z2 - z1; return sqrt(x * x + y * y + z * z); } int main(){ int cnt = 0; int his[10] = {0}; double tx,ty,tz; while(scanf("%lf %lf %lf",&tx,&ty,&tz) == 3){ if(tx == 0 && ty == 0 && tz == 0) break; t[cnt].x = tx; t[cnt].y = ty; t[cnt].z = tz; ++cnt; } for(int i = 0; i < cnt; ++i){ double tmin = INF; for(int j = 0; j < cnt; ++j){ if(i == j) continue; tmin = min(tmin,Dis(t[i].x,t[i].y,t[i].z,t[j].x,t[j].y,t[j].z)); } //因为tmin不为负,所以用(int)向下取整即可,没必要用floor() if(10 - tmin > eps) ++his[(int)(tmin)]; } for(int i = 0; i < 10; ++i){ printf("%4d",his[i]); } puts(""); return 0; }