[Leetcode]-Rectangle Area
来源:互联网 发布:人工智能电影特点评价 编辑:程序博客网 时间:2024/05/17 22:46
//题目:Find the total area covered by two rectilinear rectangles in a 2D plane.
//Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
//求两个正方形的面积(重叠地方只算一次),已知两个正方形的对角顶点
//关键在于分别求取所有4个横纵轴坐标的两个中间值
//注意:当两个正方形无重叠的情况
#include <stdlib.h>#include <stdio.h>#define min(a,b) ((a)<(b))?(a):(b)#define max(a,b) ((a)>(b))?(a):(b)int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { //正方形1对角顶点(A,B) (C,D) //正方形2对角顶点(E,F) (G,H) //横坐标: A,C,E,G //纵坐标: B,D,F,H //判断无重叠的情况 if((abs(A-G)-(C-A)-(G-E)) > 0) return (G-E)*(H-F) + (D-B)*(C-A); if((abs(E-C)-(C-A)-(G-E)) > 0) return (G-E)*(H-F) + (D-B)*(C-A); if((abs(H-B)-(D-B)-(H-F)) > 0) return (G-E)*(H-F) + (D-B)*(C-A); if((abs(D-F)-(D-B)-(H-F)) > 0) return (G-E)*(H-F) + (D-B)*(C-A); //if((A == B)&&(B == C)&&(C == D)) return (G-E)*(H-F); //if((E == F)&&(F == G)&&(G == H)) return (D-B)*(C-A); //有重叠的情况 int m=0,n=0; n = abs((min(G,C))-(max(A,E))); m = abs((min(D,H))-(max(B,F))); return (G-E)*(H-F) + (D-B)*(C-A) - m*n;}int main(){ int A = -2,B = -2,C = 2,D = 2,E = -2,F = -2,G = 2,H = 2; int area = computeArea( A, B, C, D, E, F, G, H); printf("area is :%d\n",area);}
1 0
- leetcode Rectangle Area
- [LeetCode] Rectangle Area
- leetcode--Rectangle Area
- [LeetCode]Rectangle Area
- [leetcode] Rectangle Area
- Rectangle Area - LeetCode 223
- leetcode-Rectangle Area[math]
- leetCode 之 Rectangle Area
- leetcode 223: Rectangle Area
- [leetcode]Rectangle Area
- leetcode[223]:Rectangle Area
- LeetCode Rectangle Area
- [leetcode][math] Rectangle Area
- #leetcode#Rectangle Area
- [LeetCode] Rectangle Area
- LeetCode: Rectangle Area
- Leetcode 223 Rectangle Area
- [Leetcode]-Rectangle Area
- 代码实现 Autolayout
- 百度地图开发界面出现格网
- OC下NSLog的格式输出对照表
- HTTP API选择WinInet or WinHttp?
- Swift函数
- [Leetcode]-Rectangle Area
- MajorUpgrade升级不能卸载旧版本
- 完胜汪涵、李咏、毕福剑 何炅被评综艺一哥/图
- [SharePoint 2010– InfoPath]通过REST Web Service在表单中嵌入地图
- Android使用volley发送带参数的post请求
- linux设置开机启动项
- Web端对于音频文件的支持
- Android APP通用型拒绝服务、漏洞分析报告
- java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet