poj2780解題報告

來源:互聯網
上載者:User
Linearity
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5999   Accepted: 1287

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

50 02 00 21 12 2

Sample Output

3
題意:給定n個點,判斷最多有多少個點共線
思路:把以前寫的1118拿出來改了下數組大小交上去了,1100多MS,思路是將每個點分別作為基點,將其餘點的
座標變換到以這個點為原點的座標繫上,求出每個點在這個座標繫上的斜率然後排序(分母為0的拿出來單獨處理),
然後一次迴圈就能判斷出多少個點的斜率一樣然後再求出最多共線的點。。。
#include<iostream>#include<algorithm>#include<stdio.h>using namespace std;int main(){int n;while(cin>>n && n){int x[1010],y[1010];     int i,j,k,max=2;for(i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);for(k=1;k<=n;k++){int ns=1;double xy[1010];int ed=0;for(i=1;i<=n;i++)if(x[i]-x[k]==0)ed++;elsexy[ns++]=(double)(y[i]-y[k])/(double)(x[i]-x[k]);sort(&xy[1],&xy[ns]);int zhongjie=2;int sum=2;double comp=xy[1];for(i=2;i<ns;i++)if(comp==xy[i])sum++;else{comp=xy[i];if(sum>zhongjie)zhongjie=sum;sum=2;}if(sum>zhongjie)zhongjie=sum;if(zhongjie>max)   max=zhongjie;if(ed>max)max=ed;}cout<<max<<endl;}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.