Fencing the ground
Two non-overlapping fields need to be fenced together to guard from cattle. The fence is always a single rectangle, which cover the two fields exactly.
Given the left bottom coordinate, length and width of the two fields, write a program to find dimension of the fence. Print “Invalid Input” if the fields overlap.
Input 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 Format:
Output consists of 4 integers that correspond to x, y, l and w of the Union rectangle.
Sample Input 1:
0 2 4 3
4 0 2 8
Sample Output 1:
0 0 6 8
Sample Input 2:
0 2 4 3
3 0 2 8
Sample Output 2:
Invalid Input
//PROGRAM
#include<stdio.h>
int main()
{
int x1,x2,y1,y2,l1,l2,w1,w2,xmax,xmin,ymax,ymin;
scanf("%d %d %d %d",&x1,&y1,&l1,&w1);
scanf("%d %d %d %d",&x2,&y2,&l2,&w2);
xmin=x1 < x2 ? x1 : x2;
ymin=y1 < y2 ? y1 : y2;
int b=l1+x1;
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;
if((l1+l2)>l)
printf("Invalid Input");
else
printf("%d %d %d %d",xmin,ymin,l,w);
return 0;
}
Comments
Post a Comment