85. Three Points On A Line

来源:互联网 发布:windows10怎么设置网络 编辑:程序博客网 时间:2024/06/05 02:26

时间限制 1000 ms 内存限制 65536 KB

题目描述

Given points on a 2D plane, judge whether there’re three points that locate on the same line.

输入格式

The number of test cases T(1≤T≤10) appears in the first line of input.

Each test case begins with the number of points N(1≤N≤100). The following N lines describe the coordinates (xi,yi) of each point, in accuracy of at most 3 decimals. Coordinates are ranged in [−104,104].

输出格式

For each test case, output Yes if there’re three points located on the same line, otherwise output No.

输入样例

2
3
0.0 0.0
1.0 1.0
2.0 2.0
3
0.001 -2.000
3.333 4.444
1.010 2.528

输出样例

Yes
No

通过代码

#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){    int t=0;    int n=0;    double *x;    double *y;    double x_t_1, x_t_2;    double y_t_1, y_t_2;    int c;    int k;    int i,j;        k = scanf("%d", &t);         if(t<1 || t>10){            return 0;         }         for(c=0;c<t;c++)         {            k = scanf("%d", &n);            if(n<1 || n>100){                return 0;            }            x = (double *)malloc(sizeof(double)*n);             y = (double *)malloc(sizeof(double)*n);             memset(x, 0, sizeof(double)*n);            memset(y, 0, sizeof(double)*n);            if(x == NULL || y == NULL){                return 0;            }                       j=0;            while(j<n)            {                scanf("%lf %lf", &x[j], &y[j]);                if( -10000.0 <x[j] <10000.0 && -10000.0 <y[j] <10000)                {                    j++;                }else{                    return 0;                }            }            //计算            for(i=0;i<n;i++)            {                for(j=i+1;j<n;j++)                {                    for(k=j+1;k<n;k++)                    {                        x_t_2 =x[i]*y[j]+y[i]*x[k]+x[j]*y[k];                        x_t_2 = x_t_2 - x[k]*y[j] - x[j]*y[i]-x[i]*y[k];                        if(x_t_2 > -0.0000001 && x_t_2 < 0.0000001)                        //if(fabs(x_t_2 ) <= 1e-15)                        {                            printf("Yes\n");                                goto next_test;                                                     }           //注,多余的代码删除了,可能会影响系统测试结果                     }                }            }            printf("No\n");next_test:             free(x);             free(y);        }    return 0;}

错误答案

/*USER_ID: test#liuzhuchen2016PROBLEM: 85SUBMISSION_TIME: 2016-02-27 11:01:13*/#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){    int t=0;    int n=0;    double *x;    double *y;    double x_t_1, x_t_2;    double y_t_1, y_t_2;    int c;    int k;    int i,j;        k = scanf("%d", &t);         if(t<1 || t>10){            return 0;         }         for(c=0;c<t;c++)         {            k = scanf("%d", &n);            if(n<1 || n>100){                return 0;            }            x = (double *)malloc(sizeof(double)*n);            y = (double *)malloc(sizeof(double)*n);            memset(x, 0, sizeof(double)*n);            memset(y, 0, sizeof(double)*n);            if(x == NULL || y == NULL){                return 0;            }                      j=0;            while(j<n)            {                scanf("%f %f", &x[j], &y[j]);                if( -10000.0 <x[j] <10000.0 && -10000.0 <y[j] <10000)                {                    j++;                }else{                    return 0;                }            }            //计算            for(i=0;i<n;i++)            {                for(j=i+1;j<n;j++)                {                    //x_t_1 = x[i]-x[j];                    //y_t_1 = y[i]-y[j];                                       for(k=j+1;k<n;k++)                    {                        x_t_2 =x[i]*y[j]+y[i]*x[k]+x[j]*y[k];                        //printf("%d\n", x_t_2);                        x_t_2 = x_t_2 - x[k]*y[j] - x[j]*y[i]-x[i]*y[k];                        //printf("%d\n", x_t_2);                        if(x_t_2 > -0.00000001 && x_t_2 < 0.00000001)                        {                            printf("Yes\n");                               goto next_test;                                                    }                                  //x_t_2 = x[i]-x[k];                        //y_t_2 = y[i]-y[k];                        /*                        if(x_t_1 == 0.0 && x_t_2 == 0.0)                        {                            if(y_t_1 == 00.0 && y_t_2 == 0.0)                            {                                printf("Yes\n");                                   goto next_test;                                                                }else if(y_t_1 != 0.0 && y_t_2 != 0.0){                                printf("Yes\n");                                   goto next_test;                            }                        }else if(x_t_1 != 0.0 && x_t_2 != 0.0){                            if(y_t_1 == 0.0 && y_t_2 == 0.0)                            {                                printf("Yes\n");                                   goto next_test;                                    }else if(y_t_1 != 0.0 && y_t_2 != 0.0){                                if( (x_t_1/x_t_2)==(y_t_1/y_t_2))                                {                                    printf("Yes\n");                                       goto next_test;                                                                        }                            }                        }else if(x_t_1 == 0.0 && x_t_2 != 0.0){                            if(y_t_1 == 0.0 && y_t_2 != 0.0)                            {                                printf("Yes\n");                                   goto next_test;                                }                        }else if(x_t_1 != 0.0 && x_t_2 == 0.0){                            if(y_t_1 != 0.0 && y_t_2 == 0.0)                            {                                printf("Yes\n");                                   goto next_test;                                }                                                  }                        */                    }                }            }            printf("No\n");next_test:             free(x);             free(y);        }    return 0;}

关于证明三点共线的方法

方法一:取两点确立一条直线,计算该直线的解析式 .代入第三点坐标 看是否满足该解析式 (直线与方程).
方法二:设三点为A、B、C .利用向量证明:λAB=AC(其中λ为非零实数).
方法三:利用点差法求出AB斜率和AC斜率,相等即三点共线.
方法四:用梅涅劳斯定理.
方法五:利用几何中的公理“如果两个不重合的平面有一个公共点,那么它们有且只有一条过该点的公共直线”.可知:如果三点同属于两个相交的平面则三点共线.
方法六:运用公(定)理 “过直线外一点有且只有一条直线与已知直线平行(垂直)”.其实就是同一法.
方法七:证明其夹角为180°.
方法八:设A B C ,证明△ABC面积为0.
方法九:帕普斯定理.
方法十:利用坐标证明。即证明x1y2=x2y1.
方法十一:位似图形性质.

0 0