2020牛客多校第三场C题Operation Love(叉积 模拟几何)

题目链接 https://ac.nowcoder.com/acm/contest/5668/C

题意:输入20个坐标,判断是左手还是右手,手的大小不变。

题解:我们根据边长可以找到最底下一条边的点和大拇指的最左边的点。叉积一下判断正负就可以判断是左手还是右手。

找点暴力求解就可以,下面解释一下叉积就是把线段看成向量,就是高中学的向量的乘积。

OA=(x1,y1)  OB=(x2,y2)
定义叉积:OA×OB=x1y2 - x2y1。

叉积的作用
1:确定三角形的面积,叉积相乘除以2

2:已知直线上的两点s、e,可以求出ax+by+c=0的参数
a = s.y-e.y;
b = e.x-s.x;
c = s×e;

大佬归纳 https://blog.csdn.net/Danliwoo/article/details/49836983?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-4.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-4.nonecase

 

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=20+7;
const ll mod =1e9+7;
int t;
double x[maxn],y[maxn];
double cal(double x1,double y1,double x2,double y2){
    return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}
int main(){
    scanf("%d",&t);
    while(t--){
        double  ma=-1;
        int pos1,pos2,l,mid,r;
        for(int i=1;i<=20;i++){
            scanf("%lf%lf",&x[i],&y[i]);
        }
        x[0]=x[20],y[0]=y[20];
        x[21]=x[1],y[21]=y[1];
        x[22]=x[2],y[22]=y[2];
        for(int i=1;i<=20;i++){
            double len=cal(x[i],y[i],x[i+1],y[i+1]);
            if(len>ma){
                ma=len;
                pos1=i,pos2=i+1;
            }
        }
        if(cal(x[pos1],y[pos1],x[pos1-1],y[pos1-1])<cal(x[pos2],y[pos2],x[pos2+1],y[pos2+1])){
            l=pos1-1,mid=pos1,r=pos2;
        }else{
            l=pos2+1,mid=pos2,r=pos1;
        }
        if((x[r]-x[mid])*(y[l]-y[mid])-(x[l]-x[mid])*(y[r]-y[mid])<0){
            printf("left\n");
        }else{
            printf("right\n");
        }
    }
    return 0;
}

 

posted @ 2020-08-05 17:28  杰瑞与汤姆  阅读(161)  评论(0编辑  收藏  举报