85. Three Points On A Line
题目描述
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 (x i ,y i ) of each point, in accuracy of at most 3 decimals. Coordinates are ranged in [−10 4 ,10 4 ] .
输出格式
For each test case, output Yes
if there're three points located on the same line, otherwise outputNo
.
输入样例
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
三点共线,枚举所有的3点,求斜率,一样则存在,做好标记然后退出枚举即可。还有一种做法是求出一组后,把计算结果存起来,减少计算量。
#include<stdio.h>
int main(){
int t,n,i,j,k;
double x[105],y[105];
for(scanf("%d",&t);t--;){
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf%lf",x+i,y+i);
int sig=0;
for(i=0;i<n&&!sig;i++)
for(j=i+1;j<n&&!sig;j++){
for(k=j+1;k<n&&!sig;k++){
if((y[k]-y[i])/(x[k]-x[i])==(y[j]-y[i])/(x[j]-x[i]))sig=1;
}
}
if(sig)printf("Yes\n");
else printf("No\n");
}
return 0;
}