进阶8:平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴。对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积。输入仅包含两行,每行描述一个矩形。在每行中,给出矩形的一对相对顶点的坐标。输出仅包含一个实数,为交的面积,保留到小数后两位。
- 思路1:我想找到重叠矩形的对角定点来计算重叠面积,令xy不断递增和记录,最后找到重叠的最小顶点和最大顶点。
- 错误之处为:1. 输出含小数说明输入数据并非整形,2. 使用了对角顶点xy同时递增的方式,但是只能用在正方形的图案上,长方形的对角顶点不一定正好对齐。
- 思路2:第一个坐标为左下角坐标,第二个坐标为右上角坐标,因为左下坐标越靠近右上坐标面积越小且其坐标值越大,同理右上坐标要取较小的x和y构成新坐标
- 错误之处:考虑了右上左下的输出和左上右下的调整,但是没有做出右上左下的情况的处理。综合起来即第一点取较小的xy,而第二点取较大的xy即可。
- 错误之处:没有考虑检测无交集的情况。
- 思路:当第二个图形的左侧点x轴大于前图形的x轴,或第二个图形的右侧点y轴小于前图形的y轴
cpp
#include <bits/stdc++.h>
using namespace std;
void Check(vector<pair<double,double>> &xy){
double x1 = xy[0].first, y1 = xy[0].second;
double x2 = xy[1].first, y2 = xy[1].second;
xy[0].first = min(x1,x2);xy[0].second = min(y1,y2);
xy[1].first = max(x1,x2);xy[1].second = max(y1,y2);
return ;
}
int main(){
vector<pair<double,double>> A,B;
double a,b;
int count=0;
while(cin>>a>>b){
pair<double,double> p = {a, b};
if(count==0||count==1)A.push_back(p);
if(count==2||count==3)B.push_back(p);
count++;
}
//第一个坐标为左下角坐标,第二个坐标为右上角坐标
Check(A);Check(B);
//检测是否不重叠
if (B[1].first < A[0].first || // B 在 A 左边
B[0].first > A[1].first || // B 在 A 右边
B[1].second < A[0].second || // B 在 A 下边
B[0].second > A[1].second) { // B 在 A 上边
cout << "0.00" << endl;system("pause");
return 0;
}
//构建新的左下角坐标
//x和y越大,越靠近右上角坐标
double leftx,lefty;
if(A[0].first>B[0].first)leftx=A[0].first;
else leftx=B[0].first;
if(A[0].second>B[0].second)lefty=A[0].second;
else lefty=B[0].second;
pair<double,double> left = {leftx,lefty} ;
//--------------------------------
//同理右上角越靠近左下角,那么其xy越小
double rightx,righty;
if(A[1].first<B[1].first)rightx=A[1].first;
else rightx=B[1].first;
if(A[1].second<B[1].second)righty=A[1].second;
else righty=B[1].second;
pair<double,double> right = {rightx,righty} ;
double edgey = right.second - left.second;
double edgex = right.first - left.first;
cout<<fixed<<setprecision(2)<<edgey*edgex;
system("pause");
}
This paper has two main contributions:
• We show that open-vocabulary neural machine translation is possible by encoding (rare) words via subword units. We find our architecture simpler and more effective than using large vocabularies and back-off dictionaries (Jean et al., 2015; Luong et al., 2015b).
• We adapt byte pair encoding (BPE) (Gage, 1994), a compression algorithm, to the task of word segmentation. BPE allows for the representation of an open vocabulary through a fixed-size vocabulary of variable-length character sequences, making it a very suitable word segmentation strategy for neural network models.
- 本文有两个主要贡献:
- 我们展示了证明 通过子词单元来对罕见词编码可以实现 的开放词汇表的神经机器翻译。我们发现我们的架构比使用大型词汇表和后退词典的结构更简单和更有效。
- 我们改写了将 对词片段任务的字节对编码BPE的压缩算法应用于词分割任务 。BPE能够 使得表示 开放词汇的表示通过可变长度字符序列构成 的固定尺寸词汇表,这使其成为用于 使其对神经网络模型非常合适的词分段策略。
- adapt 适应/改编, adapt A to BA用于B
- allows for **使......成为可能、**能够实现、为......创造条件
- make it sth 使其成为sth
- segmentation 分段、segment 片段