链接给定 n 个坐标,求其中 3个坐标能表示一个等腰三角形的组数。 三点共线不算三角形,等边三角形为特殊的等腰三角形。用c++代码实现
时间: 2023-05-25 16:02:49 浏览: 68
C++代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const double eps = 1e-8;
const int maxn = 105;
struct Point {
double x, y;
}a[maxn];
bool equal(double x, double y) {
return fabs(x - y) < eps;
}
double dis(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main() {
int n;
scanf("%d", &n);
int cnt = 0;
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &a[i].x, &a[i].y);
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
for (int k = j + 1; k <= n; k++) {
double AB = dis(a[i], a[j]);
double AC = dis(a[i], a[k]);
double BC = dis(a[j], a[k]);
if (equal(AB, AC) || equal(AB, BC) || equal(AC, BC)) {
cnt++;
}
}
}
}
printf("%d\n", cnt);
return 0;
}