2099:给出一段折线,求一个能包住次折线的最小矩形。
思路:找出最左最右的x值,最上最下的y值即可。
由于整数范围内都合法,初始时有些麻烦。因此设了一个变量代表第一次赋值。
思路:找出最左最右的x值,最上最下的y值即可。
由于整数范围内都合法,初始时有些麻烦。因此设了一个变量代表第一次赋值。
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int southwest_x;
int southwest_y;
int northeast_x;
int northeast_y;
int x;
int y;
bool isend=false;
bool isfirst;
while(!isend)
{
southwest_x=0;
southwest_y=0;
northeast_x=0;
northeast_y=0;
isfirst=true;
while(1)
{
cin>>x;
cin>>y;
if(x==0&&y==0)
{
if(!(southwest_x||southwest_y||northeast_x||northeast_y))
{
isend=true;
break;
}
else
break;
}
if(isfirst)
{
southwest_x = x;
southwest_y = y;
northeast_x = x;
northeast_y = y;
isfirst=false;
}
if(x<=southwest_x)
southwest_x = x;
if(y<=southwest_y)
southwest_y = y;
if(x>=northeast_x)
northeast_x=x;
if(y>=northeast_y)
northeast_y=y;
}
if(!isend)
cout<<southwest_x<<" "<<southwest_y<<" "<<northeast_x<<" "<<northeast_y<<endl;
}
}