码迷,mamicode.com
首页 > 其他好文 > 详细

蓝桥杯 BASIC-18 矩形面积交(线段重叠)

时间:2015-03-21 14:08:35      阅读:843      评论:0      收藏:0      [点我收藏+]

标签:acm   蓝桥杯   

【思路】:将各个边平行到x、y轴上,重叠部分相乘得面积。getLen中是两条线相交的各种判断。注意:因为输入一个矩形的两个点并没有说先输入左下再输入右上(测试数据也确实有先输入了右上),所以一定要两两排序。否则会出现a大于b的情况。

技术分享

【AC代码】:

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cstring>
using namespace std;

double getLen(double x[])
{
	double a = x[0], b = x[1], c = x[2], d = x[3];
	if (b < c)
	{
		return 0.0;
	}
	else if (b >= c && b <= d)
	{
		if (a > c)
			return b-a;
		else
			return b-c;
	}
	else if (b > d)
	{
		if (a <= c)
			return d-c;
		else if (a > c && a < d)
			return d-a;
		else
			return 0.0;
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	double x[4], y[4];
	int  i = 0;
	
	//input
	for (i = 0; i < 4; i++)
	{
		cin >> x[i] >> y[i];
	}
	
	//get len
	sort(x, x+2);
	sort(x+2, x+4);
	sort(y, y+2);
	sort(y+2, y+4);
	double len_x = getLen(x);
	double len_y = getLen(y);
	
	//cout << len_x << " " << len_y << endl;
	
	cout << fixed << setprecision(2) <<  len_x*len_y;
}

线段相交曾经看过,应该有更好的方法。这个代码写的很烂。

蓝桥杯 BASIC-18 矩形面积交(线段重叠)

标签:acm   蓝桥杯   

原文地址:http://blog.csdn.net/weijj6608/article/details/44514453

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!