Input and Output Format:
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. 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.
Output consists of 4 integers that correspond to x, y, l and w of the Union rectangle.
Sample Input :
3 8 1515
2 6 10 10
Sample Output:
2 6 16 17
here is my code it gets validated for some test cases and it is not accepted when I submit it. I'm trying this on an online coding website. here is my code.
#include<stdio.h>
#include<math.h>
int main()
{
int x1,y1,x2,y2,l1,w1,l2,w2,x3,y3,l3,w3;
scanf("%d %d %d %d",&x1,&y1,&l1,&w1);
scanf("\n%d %d %d %d",&x2,&y2,&l2,&w2);
if(x1<x2)
x3=x1;
else
x3=x2;
if(y1<y2)
y3=y1;
else
y3=y2;
if(x1==x2)
{
if(l1<l2)
w3=l2;
else
w3=l1;
}
if(y1==y2)
{
// printf("inp");
if(w1<w2)
{
w3=w2;
//printf("%d",w3);
}
else
{
w3=w1;
}
}
if(x1<x2)
l3=l2+fabs(x1-x2);
else if(x2<x1)
l3=l1+fabs(x1-x2);
if(y1<y2)
w3=w2+fabs(y1-y2);
else if(y2<y1)
w3=w1+fabs(y1-y2);
printf("%d ",x3);
printf("%d ",y3);
printf("%d ",l3);
printf("%d",w3);
return 0;
}
if anyone have alternative logic then tell me.
This is assuming that your widths are in the x direction and your lengths are in the y direction. If it is the other way, it should not be too hard to change.