poj1118:多点共线几何问题

代码

不知道怎么刷着就到这题了。

Lining Up
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 24677 Accepted: 7736

Description

"How am I ever going to solve this problem?" said the pilot.

Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and she had to fly over as many points as possible. All points were given by means of integer coordinates in a two-dimensional space. The pilot wanted to know the largest number of points from the given set that all lie on one line. Can you write a program that calculates this number?

Your program has to be efficient!

Input

Input consist several case,First line of the each case is an integer N ( 1 < N < 700 ),then follow N pairs of integers. Each pair of integers is separated by one blank and ended by a new-line character. The input ended by N=0.

Output

output one integer for each input case ,representing the largest number of points that all lie on one line.

Sample Input

5
1 1
2 2
3 3
9 10
10 11
0

Sample Output

3

Source

题意:
给出几个坐标值均为整数的点,求最多有多少点共线。输入包含多组数据。
做法:
借鉴了大牛们的做法。先把各点排序,然后逐点求斜率,输出最多的斜率相同数出现次数即可。我很无耻地加了一个特判。
代码:
/*Source Code
Problem: 1118 User: aclolicon
Memory: 420K Time: 94MS
Language: G++ Result: Accepted

Source Code
*/
#include<cstdio>
#include<algorithm>
#include<cmath>
#define MAXN 888
#define EPS 1e-9
using namespace std;
int n, maxi = 0, cnt;
double s[MAXN];

struct point{
int x, y;
}p[MAXN];

bool cmp (point a, point b){
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}

void solve(){
for (int i = 0; i < n; i++){
cnt = 2;
int t = 0;
for (int j = i + 1; j < n; j++){
int y = p[j].y - p[i].y;
int x = p[j].x - p[i].x;
s[t++] = (double)y / (double)x;
}
sort(s, s + t);
for (int j = 1; j < t; j++){
if (fabs(s[j] - s[j - 1]) < EPS) cnt++;
else cnt = 2;
maxi = max(cnt, maxi);
}
}
printf("%d\n", n == 2? 2: maxi);
}

void init(){
maxi = 0;
for (int i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y);
sort(p, p + n, cmp);
}

int main(){
while (~scanf("%d", &n)){
if (n == 0) break;
init();
solve();
}
}

我还没有学会写个人说明!

2 条评论

    • a/b

      有家主机

发表评论

电子邮件地址不会被公开。 必填项已用*标注