You are going to read a sequence of pairs of integer numbers. Each pair represents the Cartesian coordinates of a point in a 2-dimentional plane. The first number is the x coordinate, while the second is that of y. The sequence represents a polygonal line. Your task is to draw a rectangle with minimal length of sides that exactly surrounds the polygonal line. The sides of the rectangle are parallel to x- and y-axis, respectively.
Input
Input consists of several test cases. For each case, a sequence of coordinates is given. Each pair of x and y occupies a line, with |x| and |y| less than 2^31. The sequence is terminated with a pair of 0's. Note that (0, 0) will never be considered as a point
on any of the polygonal lines. An empty polygonal line signals the end of input.
Output
For each test case, print in one line two pairs of numbers, which are the south-west and north-east corners of the surrounding rectangle. The numbers must be separated by one space as is indicated in the samples.
Sample Input
12 56
23 56
13 10
0 0
12 34
0 0
0 0
Sample Output
12 10 23 56
12 34 12 34
int main()
{
int x,y;
while(scanf("%d%d",&x,&y))
{
if(x==0&&y==0)
break;
int minx,miny,maxx,maxy;
minx=maxx=x;
miny=maxy=y;
while(1)
{
scanf("%d%d",&x,&y);
if(x==0&&y==0)
break;
if(x<minx)
minx=x;
if(x>maxx)
maxx=x;
if(y<miny)
miny=y;
if(y>maxy)
maxy=y;
}
printf("%d %d %d %d\n",minx,miny,maxx,maxy);
}
return 0;
}
本文详细阐述了如何通过读取输入坐标序列来构建一个多边形,并进一步计算并输出能够完全包围该多边形的最小面积矩形的坐标。此算法巧妙地利用循环和条件判断,确保在输入序列终止时准确输出结果。
961

被折叠的 条评论
为什么被折叠?



