[HDU] [1432] Lining Up

Problem Description

Loading...

Problem Analysis

Find the maximum number of collinear points

Use slopes to determine collinearity
Dividing directly didn’t seem to work
just convert it to cross multiplication

Remember to count the two points used as the base pair
so we need res+2

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
struct node {
	int x,y;
} a[720];
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		for(int i=0; i<n; i++)
			scanf("%d %d",&a[i].x,&a[i].y);
		int res=0;
		for(int i=0; i<n; i++) {
			for(int j=i+1; j<n; j++) {
				int cnt=0;
				for(int k=j+1; k<n; k++) {
					if((a[k].x-a[i].x)*(a[j].y-a[i].y)==(a[k].y-a[i].y)*(a[j].x-a[i].x))
						cnt++;
				}
				if(cnt>res)
					res=cnt;
			}
		}
		printf("%d\n",res+2);
	}
	return 0;
}

Problem link: 【杭电】[1432]Lining Up

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...