POJ 2606 Rabbit hunt(我的水题之路——斜率最多)

Rabbit hunt
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6159   Accepted: 3008

Description

A good hunter kills two rabbits with one shot. Of course, it can be easily done since for any two points we can always draw a line containing the both. But killing three or more rabbits in one shot is much more difficult task. To be the best hunter in the world one should be able to kill the maximal possible number of rabbits. Assume that rabbit is a point on the plane with integer x and y coordinates. Having a set of rabbits you are to find the largest number K of rabbits that can be killed with single shot, i.e. maximum number of points lying exactly on the same line. No two rabbits sit at one point.

Input

An input contains an integer N (2<=N<=200) specifying the number of rabbits. Each of the next N lines in the input contains the x coordinate and the y coordinate (in this order) separated by a space (-1000<=x,y<=1000).

Output

The output contains the maximal number K of rabbits situated in one line.

Sample Input

6
7 122
8 139
9 156
10 173
11 190
-100 1

Sample Output

5

Source

Ural State University collegiate programming contest 2000

有n个点,问最多有多少个点共线。

直接copy的1118的代码。解法详见: POJ 1118的解题报告

注意点:
1)两个题目的结束条件不一样。(1OLE)

代码(1AC 1OLE):
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

int aid[1000][2];
float xielv[1000];

int main(void){
    int i, j, k;
    int max, tmp;
    int n;

    while (scanf("%d", &n) != EOF){
        memset(aid, 0, sizeof(aid));
        for (i = 0; i < n; i++){
            scanf("%d%d", &aid[i][0], &aid[i][1]);
        }
        max = 2;
        for (i = 0; i < n - 1; i++){
            for (j = i + 1, k = 0; j < n; j++){
                if (aid[j][0] == aid[i][0]){
                    xielv[k++] = 32767;
                }
                else{
                    xielv[k++] = (float)(aid[j][1] - aid[i][1]) / (float)(aid[j][0] - aid[i][0]);
                }
            }
            sort(xielv, xielv + k);
            for (j = 1, tmp = 2; j <= k; j++){
                if (xielv[j] == xielv[j - 1]){
                    tmp ++;
                    if (tmp > max){
                        max = tmp;
                    }
                }
                else{
                    tmp = 2;
                }
            }
        }
        printf("%d\n", max);
    }
    return 0;
}


你可能感兴趣的:(kill,Integer,input,each,float,output)