作者:小代码
时间:2013年7月26日18:54:27
IDE:CodeBlocks
-
题目描述:
-
给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内。长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内。
-
输入:
-
测试输入包含若干测试用例,每个测试用例由一系列坐标组成,每对坐标占一行,其中|x|和|y|小于 231;一对0 坐标标志着一个测试用例的结束。注意(0, 0)不作为任何一个测试用例里面的点。一个没有点的测试用例标志着整个输入的结束。
-
输出:
-
对每个测试用例,在1行内输出2对整数,其间用一个空格隔开。第1对整数是长方形框左下角的坐标,第2对整数是长方形框右上角的坐标。
-
样例输入:
-
12 56 23 56 13 10 0 0 12 34 0 0 0 0
-
样例输出:
-
12 10 23 56 12 34 12 34
C语言链表通过版:
#include<stdio.h>
#include<stdlib.h>
const int TRUE = 1;
const int FALSE = 0;
struct Point
{
int x;
int y;
struct Point * next;
};
typedef struct Point POINT;
typedef struct Point * pPOINT;
int main( void )
{
pPOINT creat();//建立链表
void append( pPOINT head, int x, int y );//链表追加元素
void show( pPOINT head );//输出链表
int lenOfpoint( pPOINT head );//返回链表长度
int isEmpty( pPOINT head );//判断链表是否为空
void fun( pPOINT head );//输出横坐标和纵坐标的最大最小值
void destroy( pPOINT head );//销毁链表
pPOINT head = NULL;
int x;
int y;
head = creat();
while( TRUE )
{
head = creat();
scanf("%d%d",&x,&y);
if( 0 == x && 0 == y )//如果输入的第一个点是 0 0,则结束程序
{
exit(0);
}
while( 0 != x || 0 != y )
{
append( head, x, y );//添加元素
scanf("%d%d",&x,&y);
}
fun( head );//输出结果
}
return 0;
}
//建立链表
pPOINT creat()
{
pPOINT head = (pPOINT)malloc(sizeof(POINT)*1);//头结点
head->next = NULL;
return head;
}
//输出链表
void show( pPOINT head )
{
if( TRUE == isEmpty( head ) )
{
return;
}
pPOINT temp = NULL;
temp = head->next;
while( NULL != temp )
{
printf("x = %d\ty = %d\n",temp->x,temp->y);
temp = temp->next;
}
}
//返回链表长度
int lenOfpoint( pPOINT head )
{
pPOINT temp = head->next;
int len = 0;
while( NULL != temp )
{
len++;
temp = temp->next;
}
return len;
}
//判断链表是否为空
int isEmpty( pPOINT head )
{
int blog = 0;
if( NULL == head->next )
{
blog = 1;
}
return blog;
}
//链表追加元素
void append( pPOINT head, int x, int y )
{
pPOINT New = (pPOINT)malloc(sizeof(POINT)*1);
pPOINT temp = head;
int len = lenOfpoint( head );
int i = 0;
New->x = x;
New->y = y;
New->next = NULL;
if( 0 != len )
{
temp = head;
for( i = 0; i < len; i++ )
{
temp = temp->next;
}
}
temp->next = New;
}
//输出横坐标和纵坐标的最大最小值
void fun( pPOINT head )
{
pPOINT temp = head->next;
int max_x = temp->x;
int max_y = temp->y;
int min_x = temp->x;
int min_y = temp->y;
int t;
if( 1 == lenOfpoint( head ) )
{
printf("%d %d %d %d\n",min_x,min_y,max_x,max_y);
}
else
{
while( NULL != temp )
{
if( temp->x > max_x )
{
max_x = temp->x;
}
if( temp->x < min_x )
{
min_x = temp->x;
}
if( temp->y > max_y )
{
max_y = temp->y;
}
if( temp->y < min_y )
{
min_y = temp->y;
}
temp = temp->next;
}
printf("%d %d %d %d\n",min_x,min_y,max_x,max_y);
}
}
//销毁链表
void destroy( pPOINT head )
{
pPOINT temp = head->next;
pPOINT t = NULL;
while( t )
{
t = temp->next;
free(temp);
temp = t;
}
free( temp );
head->next = NULL;
}
C语言非链表非数组通过版:
#include<stdio.h>
#include<stdlib.h>
const int TRUE = 1;
const int FALSE = 0;
int main( void )
{
void swap( int *p, int *q );//交换两个整数的值
int x;
int y;
int max_x = 0;
int max_y = 0;
int min_x = 0;
int min_y = 0;
int *p = NULL;
int *q = NULL;
while( TRUE )
{
scanf("%d%d",&x,&y);
if( 0 == x && 0 == y )
{
exit(0);
}
max_x = min_x = x;
max_y = min_y = y;
while( 0 != x || 0 != y )
{
if( x > max_x )//找最大横坐标
{
p = &max_x;
q = &x;
swap( p, q );
}
if( x < min_x )//找最小横坐标
{
p = &min_x;
q = &x;
swap( p, q );
}
if( y > max_y )//找最大纵坐标
{
p = &max_y;
q = &y;
swap( p, q );
}
if( y < min_y )//找最小纵坐标
{
p = &min_y;
q = &y;
swap( p, q );
}
scanf("%d%d",&x,&y);
}
printf("%d %d %d %d\n",min_x,min_y,max_x,max_y);
}
return 0;
}
//交换两个整数的值
void swap( int *p, int *q )
{
int temp = *p;
*p = *q;
*q = temp;
}