题目描述:
Connect the countless points with lines, till we reach the faraway yonder.
There are n points on a coordinate plane, the i-th of which being (i, yi).
Determine whether it’s possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.
输入描述:
The first line of input contains a positive integer n (3 ≤ n ≤ 1 000) — the number of points.
The second line contains n space-separated integers y1, y2, …, yn ( - 109 ≤ yi ≤ 109) — the vertical coordinates of each point.
输出描述:
Output “Yes” (without quotes) if it’s possible to fulfill the requirements, and “No” otherwise.
You can print each letter in any case (upper or lower).
输入:
5
7 5 8 6 9
5
-1 -2 0 0 -5
5
5 4 3 2 1
5
1000000000 0 0 0 0
输出:
Yes
No
No
Yes
题意:
给你一堆点,问这些点能不能组成两条平行线,使这两条平行线不重合,而且每条边至少有一个点。
题解:
拿前三个点出来算斜率就好了
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 5;
int a[maxn];
int n;
bool cal(double x){
set<double>s;
for(int i = 1; i <= n; i ++){
double k = (1.0) * a[i] - i * x;
s.insert(k);
}
if(s.size() == 2) return true;
else return false;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i = 1; i <= n; i ++) scanf("%d",&a[i]);
double k1 = double(a[3] - a[1]) / (2.0);
double k2 = double(a[2] - a[1]) / (1.0);
double k3 = double(a[3] - a[2]) / (1.0);
if(cal(k1) || cal(k2) || cal(k3)){
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}