[排序&&模拟]Lining Up uva270



 Lining Up 

``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

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input consists of N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended by a new-line character. The list of pairs is ended with an end-of-file character. No pair will occur twice.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. 
The output consists of one integer representing the largest number of points that all lie on one line.

Sample Input

1

1 1
2 2
3 3
9 10
10 11

Sample Output

3

题意:

「我要如何解决这个问题呢?」飞行员说。

这个飞行员正面临一项不容易的任务,他必须在一个危险区域中的某些定点投掷包裹。因为他只能穿越此区域一次,而且是飞直线,所以他必须尽可能的通过那些定点,以便投掷多一点包裹。所有的定点均以平面座标来表示。从这些定点的座标资料,飞行员想要知道最多有多少个点是在同一直线上的。

解法:自己一开始写了个暴力枚举的,结果超时了,后来参考了下某位同学的代码,他的思路是先求出两条直线的斜率,然后排序,斜率相等的并经过同一点的就在一条直线上。

这道题水题磨了很久,后来还因为浮点数的输入导致wa,真是不应该,引以为戒。

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

class node
{
public:
    float x,y;
}point[800];

double arry[1000];

int main()
{
    int num,maxnum,i,j,k,r,temp,t,s,d;
    char str[100];
    cin>>num;
    cin.get();
    cin.get();
    while(num--)
    {
        k=0;
        while(gets(str))
        {
            if(!str[0]) break;
            sscanf(str,"%f%f",&point[k].x,&point[k].y);
            k++;
        }
        //cout<<k<<endl;
        maxnum=0;
        for(i=0;i<k;i++)
        {
            for(j=0,r=0;j<k;j++)
            {
                if(i!=j)
                {
                    arry[r++]=(point[i].y-point[j].y)/(point[i].x-point[j].x);
                }
            }
            sort(arry,arry+r);
            for(r=0;r<k-1;r++)
            {
                for(d=r,t=r+1,temp=2;arry[d]==arry[t];d++,t++,temp++)
                    ;
                if(temp>maxnum) maxnum=temp;
            }
        }
        cout<<maxnum<<endl;
        if(num) cout<<endl;
    }
    return 0;
}



你可能感兴趣的:(排序,模拟)