原本以为精度问题,原来是最后需要一个换行。直接暴力是0.216,如果加个弱弱的排序优化一下是0.120。
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #define sqr(x) ((x)*(x)) typedef struct _Point { double x, y, z; }Point; int cmp(const void *_a, const void *_b) { Point* a = (Point *)_a; Point* b = (Point *)_b; return (int)(a->x - b->x); } int main() { Point p[5005]; int n = 0, r[15]; double x, y, z; while (scanf("%lf%lf%lf", &x, &y, &z)) { if (x+y+z < 1e-9) break; p[n].x = x; p[n].y = y; p[n].z = z; n++; } qsort(p, n, sizeof (p[0]), cmp); memset(r, 0, sizeof (r)); for (int i=0; i<n; i++) { int tmin = 10; for (int j=0; j<n; j++) { if (i == j) continue; if (p[j].x-p[i].x > 10) // 如果x上距离超过了10不再搜索 break; int tmp = (int)(sqrt(sqr(p[i].x-p[j].x) + sqr(p[i].y-p[j].y) + sqr(p[i].z-p[j].z))); tmin = tmin < tmp ? tmin : tmp; } r[tmin]++; } for (int i=0; i<10; i++) printf("%4d", r[i]); printf("\n"); return 0; }