cf 849B

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/peter_xiazhen/article/details/77799027

题目大意:判断所有点能否在两条相互平行的直线上。做法:枚举K,计算前三个点两两之间的斜率,真实值定在三者中的一个,然后一个一个判断。(枚举k,暴力排查)


//codeforces 849B
#include <stdio.h>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;
const int maxn=1005;
int y[maxn];
int n; 
bool check(double k)
{
    int flag=0,spot=-1;
    for(int i=2;i<=n;i++)
    {
        if((y[i]-y[1])==k*(i-1)) continue; //说明满足假设斜率,继续检索 
        flag=1;//说明有了两条直线
        if(spot==-1) spot=i; //第二条直线的第一个点 
        else if((y[i]-y[spot])!=k*(i-spot))
        {
            flag=0; 
            break;
        }
    }
    if(flag) return true;
    else return false;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(y,0,sizeof(y));
        for(int i=1;i<=n;i++) scanf("%d",&y[i]);
        double k1=(y[2]-y[1])*1.0;
        double k2=(y[3]-y[1])*0.5;
        double k3=(y[3]-y[2])*1.0;
        if(check(k1)||check(k2)||check(k3)) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/peter_xiazhen/article/details/77799027