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)。
                    2:要求给定的n给点,找到同一直线上点最多有几个。我们知道两个点就可以构成直线,所以我们必须去枚举每一条可能的直线,判断这时候在这条直线上的点的个数,更新最大值。
                    3:枚举,时间复杂度O(n^3)

                    4 sscanf的应用(见我博客“ACM---资料收集”)

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
#define MAXN 1010

int t , len;
int x[MAXN] , y[MAXN];
int vis[MAXN];

int solve(){
    int max = 1 , tmp_max;
    int i , j , k;
    int tmp_x1 , tmp_y1 , tmp_x2 , tmp_y2;
    for(i = 0 ; i < len ; i++){//枚举点A
        for(j = i+1 ; j < len ; j++){//枚举点B
            tmp_x1 = x[j]-x[i] ; tmp_y1 = y[j]-y[i];
            tmp_max = 2;
            for(k = j+1 ; k < len ; k++){//枚举点C
                tmp_x2 = x[k]-x[i] ; tmp_y2 = y[k]-y[i];
                if(tmp_y1*tmp_x2 == tmp_y2*tmp_x1)
                    tmp_max++;
            }
            if(max < tmp_max) max = tmp_max;//更新max
        }
    }
    printf("%d\n" , max);
}

int main(){
    //freopen("input.txt" , "r" , stdin);
    int i;
    char ch[100];
    scanf("%d%*c" , &t);
    getchar();//这里换行
    while(t--){
        i = 0;
        while(gets(ch)){
            if(!ch[0] && i) break;//注意如果gets没有读入东西则第一个为NULL即0
            sscanf(ch , "%d%d", &x[i] , &y[i]);
            i++;
        }
        len = i ;
        solve(); if(t) printf("\n");
    }
    return 0;
}
时间: 2024-09-20 16:27:40

uva 270 - Lining Up的相关文章

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 h

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 402 M*A*S*H (STL&amp;amp;list)

402 - M*A*S*H Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=343 Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean W

UVa 79 ClockHands (water ver.)

579 - ClockHands Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=520 The medieval interest in mechanical contrivances is well illustrated by the developme

算法题:uva 10535

题目大意: 一个人拿着激光枪站在坐标(x,y)处,周围有N个墙,墙的两端点坐标为(x0, y0, x1, y2).这个人朝着某个方向开枪,激光可以穿过任意数量个墙.求最多一枪能够穿过几个墙?注意 ,如果激光正好在墙的一端擦边而过,也算穿过. 思路: 看下图, 把人站的坐标看做是坐标的原点,人的正东西向为x轴,南北为y轴,正东向为0度. 然 后就可以分别计算每一个墙要朝着多少度范围内射击能射到.这样,问题就抽象成了"给n个闭区间,求 某一点覆盖的区间最多",和 uva 1398 - Me

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence