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;
}
附上运行截图

心得:对数据正确有效的处理能起到事半功倍的效果
### Depot-Tools for Windows Usage and Installation Guide #### Preparation Before Installation Before installing depot-tools on a Windows system, ensure that Python is installed because several scripts within depot-tools require this environment. Additionally, Git should be available since depot-tools heavily relies on it for version control operations. #### Downloading Depot-Tools The official repository of depot-tools can be cloned directly from Google's servers using Git. Execute the following command in PowerShell or Command Prompt to clone the depot-tools repository: ```powershell git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git ``` After cloning completes successfully[^1], add the path where depot-tools resides into the `PATH` environmental variable so commands provided by depot-tools become accessible globally across all shells used on the machine. #### Verifying Installation Correctness Once added to the PATH, open a new instance of your shell (PowerShell, CMD) and run one of the common depot-tools utilities like `gclient`. If everything has been set up correctly, no error messages will appear indicating missing dependencies or unrecognized commands. #### Common Commands Overview Depot-tools includes various useful tools such as `fetch`, which simplifies setting up repositories; `gclient sync` synchronizes projects according to configuration files defined previously through fetch or other means; while `gn` serves as an interface generator for ninja build configurations when working specifically with Chromium-based projects[^4]. #### Troubleshooting Tips If encountering issues during setup or usage, consider consulting documentation related to specific tasks intended to perform after obtaining depot-tools. For example, instructions regarding automatic downloads and builds applicable to CEF might offer insights beneficial even outside its immediate context[^2]. Similarly, experiences shared around automating processes involving ESXi hosts may provide indirect guidance concerning automation frameworks compatible with Windows environments[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值