BUPTOJ85 Three Points On A Line

来源:互联网 发布:iphone6s不能更新软件 编辑:程序博客网 时间:2024/06/06 18:21
考研党200年没码过代码了,没想到一上手这么生,还是和ACM大牛们没法比啊╮(╯▽╰)╭但好歹也是技术流,码代码还算有点自信。。。为了准备复试,抱着把北邮OJ上所有题刷一遍的心情就去了。。

PS:居然在论坛听说,有考研400分的大神,不、会、写、代、码!!

BUPT83 A+B 无压力秒杀!!!First Blood!!

BUPT84 Single Number 本来以为能轻松Double Kill的。。没想到跪在超时上了,到现在还没想明白

BUPT85 Three Points On A Line 没想到连跪两题,有辱ACM业余队员称号(低调低调) =  =

BUPT86...

不行!!必须把BUPT85做出来!!决不能再这样水下去了!!!我是要成为大牛的男人!!于是——

**********************************

上面不是分割线,是我说的话。解决了看错题、拼写错误等低端失误之后,还是答案错误。最后,我只好百度了。。。看到了一大牛写的解答。。暴力归暴力,我终于知道自己哪错了。

http://blog.csdn.net/birdstorm_s/article/details/21017411

之所以称之为大牛,是因为大牛总是在字里行间体现水平。他说有O(n^2)的算法,这个可是我百度好久之后才找到的,3SUM Hard问题。。

http://cs.smith.edu/~orourke/TOPP/P11.html#Problem.11

我确定我这不是算法问题,我怀疑是float的精度问题。。。

我判断三点共线用的斜率,但斜率就要除法,两个三位小数精度的数除完之后的精度是什么来着?好像物理实验课讲过。。可惜已经忘了。。我试了10^-4、10^-5,都报错。

大牛的判断方法把斜率相等的公式推导了一下,变成了一个乘法和减法的算式。这个好,不仅效率比除法高,而且精度易计算,10^-6。

判断方法改后,直接通过了。。仅以此文记这艰难的AC,展望更加艰难的未来╮(╯▽╰)╭

/*USER_ID: test#aa3615058PROBLEM: 85SUBMISSION_TIME: 2014-03-17 23:06:13*/#include <iostream>#include <math.h>using namespace std; typedef struct Point{    float x;    float y;}Point; bool isOnALine(Point a, Point b, Point c);float slope(Point a, Point b); int main() {    int caseCount;    bool onaline;    cin>>caseCount;    for(int i = 0; i < caseCount; i++) {        int pointCount;        cin>>pointCount;         Point ps[pointCount];        for(int j = 0; j < pointCount; j++) {            cin >> ps[j].x >> ps[j].y;        }        onaline = false;        if(pointCount >= 3) {            Point a,b,c;            for(int k = 0; k < pointCount && !onaline; k++) {                a = ps[k];                for(int l = k + 1; l < pointCount && !onaline; l++) {                    b = ps[l];                    for(int g = l + 1; g < pointCount && !onaline; g++) {                        c = ps[g];                        if(isOnALine(a,b,c)) {                            onaline = true;                            break;                        }                    }                }            }        }        if(onaline) {            cout << "Yes" << endl;        } else {            cout << "No" << endl;        }    }     return 0;} bool isOnALine(Point a, Point b, Point c) {    return (fabs(a.x * c.y + b.x * a.y + b.y * c.x - b.x * c.y - a.y * c.x - b.y * a.x) <= 1e-6);} float slope(Point a, Point b) {    return (a.y - b.y) / (a.x - b.x);}

0 0
原创粉丝点击