两个工会rectangles.write程序找到尽可能小的矩形,内含2给出矩形 [英] union of two rectangles.write a program to find the smallest possible rectangle enclosing the 2 given rectangles
问题描述
鉴于左下角(X,Y),长度(l)和宽度的坐标2的矩形(重量),写一个程序,找出可能的最小矩形包围2给定的矩形。
Given the coordinates of the lower left corner (x,y), the length (l) and width(w) of 2 rectangles, write a program to find the smallest possible rectangle enclosing the 2 given rectangles.
输入和输出格式:
输入的第一行包含用空格分隔的4个整数对应于X,Y,L和W的第一个矩形的。
The 1st line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the first rectangle.
输入的第二行包括用空格分隔的4个整数对应于X,Y,L和W的第二个矩形的。
The 2nd line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the second rectangle.
输出由对应于X,Y,L和W的联盟矩形的4个整数。
Output consists of 4 integers that correspond to x, y, l and w of the Union rectangle.
样品输入:
3 8 1515
2 6 10 10
2 6 10 10
示例输出:
2 6 16 17
2 6 16 17
我无法弄清楚逻辑....我不要求对整个方案,只希望在逻辑部分帮助...
I am not able to figure out the logic....I am not asking for the whole program,just want help on the logic part...
更新:
这里是我的计划,现在工作...感谢ü所有的帮助:)
UPDATE: here is my program which is now working...thank u all for your help :)
#include<stdio.h>
int main() {
int x1, x2, y1, y2, l1, l2, w1, w2, xmax, xmin, ymax, ymin;
scanf("%d %d %d %d\n",&x1,&y1,&l1,&w1);
scanf("%d %d %d %d\n",&x2,&y2,&l2,&w2);
xmin = x1 < x2 ? x1 : x2;
ymin = y1 < y2 ? y1 : y2;
int b = x1 + l1;
int c = x2 + l2;
xmax = b > c ? b : c;
int d = y1 + w1;
int e = y2 + w2;
ymax = d > e ? d : e;
int l = xmax - xmin;
int w = ymax - ymin;
printf("%d %d %d %d",xmin,ymin,l,w);
return 0;
}
不能在我的程序找出问题..:(
can't figure out the problem in my program.. :(
推荐答案
步骤:
- 计算两个矩形的右上角
- 您一定要排序的X坐标,而y坐标。
-
内附2给出矩形的最小矩形有:
- calculate upper-right corner of two rectangles
- You must to Sort the x's coordinates, and the y's coordinates.
The smallest rectangle enclosing the 2 given rectangles has:
- 左下角:
(X1,Y1)
- 右上角:
(X4,Y4)
- lower-left corner:
(x1,y1)
- upper-right corner:
(x4,y4)
计算大小。
请参阅图:
这篇关于两个工会rectangles.write程序找到尽可能小的矩形,内含2给出矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!