UVa 270:lining up

Source: Internet
Author: User
Tags include integer printf

Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=206

Original title:

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

Indeed, the pilot is not facing a 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 Pos Sible. All points were given by means of the integer coordinates in a two-dimensional spaces. The pilot wanted to know the largest number of points from the given set, all lie on one line. Can you write a program so calculates this number?

Your program has to be efficient!

Input

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The input begins with a single positive integer in a line by itself indicating the number of cases following, each of them as described below. This are followed by a blank line, and there are also a blank line between two consecutive. The input consists of N pairs of integers, where 1 < N < 700. Each pair of integers was 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 would occur twice.

Output

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

Sample Input

1

1 1
2 2 3 3 9
ten
10 11

Sample Output

3

The main effect of the topic:

Give a series of points, ask the maximum number of points can be connected to a line.

Analysis and Summary:

Two points to determine a line, then you can enumerate all two points of the situation, and then in accordance with the two points determined by a straight line, and then traverse the other points, to determine a few points in this line.

The way to determine whether a three point P1 (x1,y1), p2 (x2,y2), P3 (X3,Y3) is a straight line is to see whether the slope of the P1,P2 is equal to the slope of the P2,P3, the mathematical formula is (X1-X2)/(Y1-y2) = (x2-x3)/(Y2-Y3), But the direct division compares the slope the accuracy to have the loss, therefore uses the diagonal to multiply the rule, may convert this equation to (x1-x2) * (y2-y3) = (y1-y2) * (X2-X3).

Then there is the violent enumeration.

Code:

* * uva:270-lining up * time:0.888s * author:d_double * * * * * * * * */#include <cstdio> #include <cmath&gt  
;  
#include <algorithm> #define MAXN 705 using namespace std;  
struct node{int x,y;  
}ARR[MAXN];  
int nindex;  
      
Char str[1000];  
    inline void input () {nindex=0;  
        while (gets (str)) {if (!str[0]) break;  
        SSCANF (str, "%d%d", &arr[nindex].x,&arr[nindex].y);  
    ++nindex; } inline bool Is_in_line (int x1,int y1,int x2,int y2,int x3,int Y3) {return (X1-X2) * (y3-y2)-(X3-X2) * (Y1  
-y2) ==0;  
    } void Solve () {int maxnum=2;  
            for (int i=0; i<nindex; ++i) {for (int j=i+1; j<nindex; ++j) {int cnt=2;   
                    for (int k=j+1; k<nindex; ++k) {if (Is_in_line (ARR[I].X,ARR[I].Y,ARR[J].X,ARR[J].Y,ARR[K].X,ARR[K].Y))  
            ++cnt;  
        } if (Cnt>maxnum) maxnum=cnt; }} Printf ("%d\n", maxnum);  
    int main () {int T;  
    scanf ("%d%*c", &t);  
    Gets (str);  
        while (t--) {input ();  
        if (nindex==1) printf ("1\n");  
        else if (nindex==2) printf ("2\n");  
        else solve ();  
    if (T) printf ("\ n");  
return 0; }

Author: csdn Blog shuangde800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.