UVa 270:Lining Up

链接:

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

原题:

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

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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

题目大意:

给出一系列的点, 求最多有多少个点能连成一条线。

分析与总结:

两点确定一条直线, 那么就可以枚举所有的两点情况,然后在根据这两个点确定的一条直线,再遍历其它点,判断有几个点是在这条直线上的。

判断三个点p1(x1,y1),p2(x2,y2),p3(x3,y3)是否是一条直线的方法是,看p1,p2的斜率是否与p2,p3的斜率相等, 数学公式是(x1-x2)/(y1-y2)=(x2-x3)/(y2-y3),但是直接相除比较斜率的话精度会有损失,所以利用对角线想乘法则,可以把这个式子转化成(x1-x2)*(y2-y3)=(y1-y2)*(x2-x3).

接着就是暴力枚举了。

代码:

/*
 * UVa:  270 - Lining Up
 * Time: 0.888s
 * Author: D_Double
 *
 */
#include<cstdio>
#include<cmath>
#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;
}

作者:csdn博客 shuangde800

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索int
, number
, 直线
, line
, of
The
,以便于您获取更多的相关知识。

时间: 2024-10-31 05:35:41

UVa 270:Lining Up的相关文章

UVa 270 / POJ 1118 Lining Up:计算几何

270 - Lining Up Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=206 http://poj.org/problem?id=1118 ``How am I ever going to solve this problem?" sai

uva 270 - Lining Up

点击打开链接uva270 题目意思:    给定平面上的n个点,要求在同一条直线上最多几个 解题思路:    枚举所有解                      1:三个点共线的性质:A(X1,Y1),B(X2,Y2),C(X3,Y3);这个时候有(Y2-Y1)/(X2-X1) = (Y3-Y1)/(X3-X1),我们知道对于double类型是不能够直接进行比较的,所以由这个式子可以变形得到:(Y2-Y1)*(X3-X1) = (Y3-Y1)*(X2-X1).                

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di

UVa 10340:All in All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1281 原题: You have devised a new encryption technique which encodes a message by inserting between its characters randomly gener