1196: 数星星(二)(结构体专题)(最简单版本)

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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值