Linearity Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 8517 Accepted: 1969
Description
Alice often examines star maps where stars are represented by points in a plane and there is a Cartesian coordinate for each star. Let the Linearity of a star map be the maximum number of stars in a straight line. For example, look at the star map shown on the figure above, the Linearity of this map is 3, because the star 1, star 2, and star 5 are within the same straight line, and there is no straight line that passes 4 stars. You are to write a program to find the Linearity of a star map.Input
Input will contain multiple test cases. Each describes a star map. For each test case, the first line of the input contains the number of stars N (2 <= N <= 1000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0 <= X, Y <= 1000). There can be only one star at one point of the plane.Output
Output the Linearity of the map in a single line.Sample Input
5 0 0 2 0 0 2 1 1 2 2Sample Output
3Source
POJ Monthly--2006.03.26,static问题链接:POJ2780 Linearity。
题意简述:输入n,输入n个整数对,即n个坐标点,问最多共线点数是多少。
问题分析:用暴力法解决这个问题,好在计算规模不算大。
程序说明:
程序中,判断共线时,使用的是乘法,没有用除法,可以保证精确的计算结果。
这个问题与POJ1118 HDU1432 Lining Up基本上相同,只是输入数据格式略有不同。
AC的C语言程序如下:
/* POJ2780 Linearity */ #include <stdio.h> #define MAXN 1000 struct { int x, y; } p[MAXN+1]; /* point */ int main(void) { int n, ans, max, i, j, k; while(scanf("%d", &n) != EOF) { for(i=0; i<n; i++) scanf("%d%d", &p[i].x, &p[i].y); ans = 2; for(i=0; i<n; i++) for(j=i+1; j<n; j++) { max = 2; for(k=j+1; k<n; k++) if ((p[j].x - p[i].x)*(p[k].y - p[j].y) == (p[j].y - p[i].y)*(p[k].x - p[j].x)) max++; if(max > ans) ans = max; } printf("%d\n", ans); } return 0; }