题目:
题解:
int cmpfunc(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
//计算组合数*2
int every(int count) {
if (count == 1) {
return 0;
} else {
return count * (count - 1);
}
}
//计算每个锚点能产生的回旋镖总数
int part(int *d, int pointsSize) {
int Sum = 0;
int count = 1;
for (int i = 0; i < pointsSize - 1; i++) {
if (d[i] == d[i + 1]) {
count++;
} else {
Sum += every(count);
count = 1;
}
}
Sum+=every(count);
return Sum;
}
//计算总共能产生的回旋镖的数量
int numberOfBoomerangs(int** points, int pointsSize, int* pointsColSize){
if(pointsSize <= 2){
return 0;
}
int sum = 0;
for(int i = 0; i < pointsSize; i++) {
int *d = (int *)malloc((pointsSize) * sizeof(int));
int *co = d;
for(int k = 0; k < pointsSize; k++) {
*co = pow(points[i][0] - points[k][0], 2) + pow(points[i][1] - points[k][1], 2);
co++;
}
qsort(d, pointsSize, sizeof(int), cmpfunc);
sum += part(d, pointsSize);
free(d);
}
return sum;
}