题目

在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分。假定正方形的上下两条边与x轴平行。

给定两个vecotrA和B,分别为两个正方形的四个顶点。请返回一个vector,代表所求的平分直线的斜率和截距,保证斜率存在。

测试样例:

[(0,0),(0,1),(1,1),(1,0)],[(1,0),(1,1),(2,0),(2,1)]`
返回:[0.0,0.5]

解法

简单的求斜率、截距问题,既然要平分2个正方形就可以让这条直线经过两个正方形的中心。不过在这里要注意题目的测试数据可能是乱序给出的,即四个顶点但并不是按一定顺序排列的,我们在进行处理时需要先简单排序一下,代码如下:

/*
struct Point {
int x;
int y;
Point() :
x(0), y(0) {
}
Point(int xx, int yy) {
x = xx;
y = yy;
}
};*/
class Bipartition {
public:
static bool cmp(const Point &A,const Point &B)
{
if(A.x!=B.x)
return A.x<B.x;
return A.y<B.y;
} vector<double> getBipartition(vector<Point> A, vector<Point> B) {
// write code here
sort(A.begin(),A.end(),cmp);
sort(B.begin(),B.end(),cmp);
double x1=(A[0].x+A[3].x)/2;
double x2=(B[0].x+B[3].x)/2;
double y1=(A[0].y+A[3].y)/2;
double y2=(B[0].y+B[3].y)/2;
double k=(y1-y2)/(x1-x2);
double b=y1-k*x1;
res.push_back(k);
res.push_back(b);
return res;
}
private:
vector<double> res;
};

CC36:平分的直线的更多相关文章

  1. 平分的直线 牛客网 程序员面试金典 C++ Python

    平分的直线 牛客网 程序员面试金典 C++ Python 题目描述 在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分.假定正方形的上下两条边与x轴平行. 给定两个vecotrA和B ...

  2. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  3. MFC中如何画带实心箭头的直线

    工作中遇到话流程图的项目,需要画带箭头的直线,经过摸索,解决:思路如下: (1) 两个点(p1,p2)确定一个直线,以直线的一个端点(假设p2)为原点,设定一个角度 (2)以P2为原点得到向量P2P1 ...

  4. 水平可见直线 bzoj 1007

    水平可见直线 (1s 128M) lines [问题描述] 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆 ...

  5. SVG:linearGradient渐变在直线上失效的问题解决方案

    SVG开发里有个较为少见的问题. 对x1=x2或者y1=y2的直线(line以及path),比如: <path d="M200,10 200,100" stroke=&quo ...

  6. 封装 用canvas绘制直线的函数--面向对象

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. [bzoj1007][HNOI2008][水平可见直线] (斜率不等式)

    Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为 可见的,否则Li为被覆盖的. 例如,对于直线: L1:y ...

  8. [LeetCode] Line Reflection 直线对称

    Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...

  9. 点(x3,y3)到经过点(x1,y1)和点(x2,y2)的直线的最短距离

    /// <summary> /// 点(x3,y3)到经过点(x1,y1)和点(x2,y2)的直线的最短距离 /// </summary> /// <param name ...

随机推荐

  1. POJ2104 K-th Number —— 区间第k小 整体二分

    题目链接:https://vjudge.net/problem/POJ-2104 K-th Number Time Limit: 20000MS   Memory Limit: 65536K Tota ...

  2. JS遍历ChexkBoxList

    var cboxs = $("#cblAuth input[type=checkbox]"); ; ; i < cboxs.length; i++) { if (cboxs[ ...

  3. codeforces 659B B. Qualifying Contest(水题+sort)

    题目链接: B. Qualifying Contest time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. leetcode 43. Multiply Strings(高精度乘法)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. 【面试题046】求1+2+...+n

    [面试题046]求1+2+...+n 题目:     求1+2+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C).   思 ...

  6. MySQL-计算当月重新激活客户_20161013

    13号的草稿 12号的明天补充更新,最近太忙了. 客户留存率是衡量客户价值经常用的指标,可以反映客户的活跃程度,在互联网企业,尤其是现在手机端流量已经超过PC端流量,在安卓和IOS设备上在线时长的数据 ...

  7. 1130 host is not allowed to connect to

    mysql 远程访问不行解决方法 Host is not allowed to connect to this MySQL server 如果你想连接你的mysql的时候发生这个错误: ERROR 1 ...

  8. ogg概叙、架构、进程

    一. OGG 概述 OGG 全称Oracle Golden Gate. 历史: Golden Gate公司于1995年成立于美国加州旧金山,它的名称源自旧金山闻名于世的金门大桥.两位创始人Eric F ...

  9. poj2955——括号匹配

    题目:http://poj.org/problem?id=2955 区间DP. 代码如下: #include<iostream> #include<cstdio> #inclu ...

  10. win7 64位搭建Mantis 缺陷管理系统

    什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...