1197
题名: 数星星(二)(结构体专题)
解题思路:
💡
先输入所有的数字,然后使用3个for循环,遍历出连续出现的3个点→p1和p2斜率,p2和p3的斜率
1.使用 comp → 计算斜率
2.如果相同 则返回1 否则返回0 不进行计算
3.其中 count 来计算 出现的相同斜率的次数
4.max表示 斜率出现最多的次数
#include<stdio.h>
#include<string.h>
#define MAX 10000
struct s{
double x;
double y;
};
int comp(struct s p1,struct s p2,struct s p3){
if((p2.x-p1.x)*(p3.y-p1.y) - (p3.x-p1.x)*(p2.y-p1.y) == 0)
return 1; // 计算斜率 (x1-x0)/(x2-xo) = (y1-y0)
else
return 0;
}
int main(void){
int n;
scanf("%d",&n);
struct s a[n];
int i,j;
for(i=0;i<n;i++){
scanf("%lf %lf",&a[i].x,&a[i].y);
}
int max=0,k;
int count=0;
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
count =2; // 一定会有2个点在一条线
for(k=j+1;k<n;k++){
if(comp(a[i],a[j],a[k]) == 1){
++count;
}
}
if(count > max){
max = count;
}
}
}
printf("%d",max);
return 0;
}