给定 n 个坐标,求其中 3 个坐标能表示一个等腰三角形的组数 两重循环C语言写
时间: 2023-05-25 08:02:41 浏览: 149
基于循环嵌套以及函数打印一个等腰三角形
#include <stdio.h>
int main() {
int n, i, j, cnt = 0;
scanf("%d", &n);
int x[n], y[n];
for (i = 0; i < n; i++) {
scanf("%d %d", &x[i], &y[i]);
}
for (i = 0; i < n-2; i++) {
for (j = i+1; j < n-1; j++) {
if (x[i] == x[j] || y[i] == y[j]) continue;
double k1 = (double)(y[i]-y[j])/(double)(x[i]-x[j]);
int l, cnt_l = 0, cnt_r = 0;
for (l = j+1; l < n; l++) {
if (x[l] == x[i] || x[l] == x[j] || y[l] == y[i] || y[l] == y[j]) continue;
double k2 = (double)(y[i]-y[l])/(double)(x[i]-x[l]);
if (k1 == k2 || (k1*k2 == -1.0)) {
if ((x[l] < x[i] && x[l] < x[j]) || (x[l] > x[i] && x[l] > x[j])) {
cnt_l++;
}
else if ((x[l] > x[i] && x[l] < x[j]) || (x[l] < x[i] && x[l] > x[j])) {
cnt_r++;
}
}
}
cnt += cnt_l * cnt_r;
}
}
printf("%d\n", cnt);
return 0;
}
阅读全文