C语言--Building a New Depot

题目链接:点击打开链接

Description

Advanced Cargo Movement, Ltd. is successfully expanding. In order to meet new demands on truck maintenance, the management of the company decided to build a new truck depot. A suitable lot for building a depot was purchased and a construction company ``Masonry and Fences for Future, Ltd.'' was hired to build a depot.

The area of the depot will be enclosed by a fence. The fence is supposed to enclose a connected area of a lot and each part of the fence follows North-South or East-West direction. At each place where the fence changes its direction, there is a single post. The posts are only at points where the fence changes the direction, i.e., there are no unnecessary posts. When MFF workers have built all of the posts, they lost the plan of a depot being built. At this moment, they asked you for a help.

Given the coordinates of all the posts, your task is to compute the length of the fence.

Input

The input consists of several blocks. The first line of each block contains a single number P, 1 <= P <= 100 000. P is the number of posts which have been built. Each of the following P lines contains two integers X and Y, 0 <= X, Y <= 10 000, which represent coordinates of the posts in MFF internal units (which no one else is able to understand). No two posts have the same coordinates.

Each block is followed by a single empty line and the input is terminated by a line containing a single number 0.

Output

Output contains a single line for each block. The line should contain the text "The length of the fence will be L units.", where L is replaced by an actual length of the fence. You can assume that the fence can always be built.

Sample Input

6
1 1
1 3
3 3
2 1
3 2
2 2

0

Sample Output

The length of the fence will be 8 units.


题目大意:说直白点就是给出一系列的坐标,然后计算就水平竖直的栅栏连接,问需要栅栏多少米?
题目分析:很明显这又涉及到结构体的定义与排序,直接一个结构体搞定。
重点及注意事项:1.对结构体的定义排序 
                               2.在计算时如何防止如1,3;2,4;2,5;之类的坐标,重复相加了,怎么办?
                               3.排序方法的使用(防止超时);

不多说AC代码----方法仅供产考
#include <stdio.h>
#include <algorithm>
using namespace std;
struct point
{
	int x,y;
}
p[1000002];
bool cmp1(point a,point b)
{
	if(a.y == b.y)
		return a.x < b.x;
	return a.y < b.y;//以y坐标为基准升序排列坐标
}
bool cmp2(point a,point b)
{
	if(a.x==b.x) 
		return a.y<b.y;
	return a.x<b.x;//以x坐标为基准升序排列坐标
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF&&n!=0)
	{
		for(int i=0;i<n;i++)
			scanf("%d%d",&p[i].x,&p[i].y);
		int s=0;
		sort(p,p+n,cmp1);
		for(int j=0;j<n;j+=2)
			s+=(p[j+1].x-p[j].x);//数据每两个为一组,避免了重复处理数据,导致答案错误
		sort(p,p+n,cmp2);
		for(int k=0;k<n;k+=2)
			s+=(p[k+1].y-p[k].y);//x,y的长度分开计算,与上面的不同基准排序相照应
		printf("The length of the fence will be %d units.\n",s);  	
	}
	return 0;
}
附上运行截图

心得:对数据正确有效的处理能起到事半功倍的效果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值