【2024.2.5练习】独木桥

这篇文章介绍了一道编程题目,要求在C++中读取士兵数量和每个士兵的位置,计算并输出所有士兵到最远和最近其他士兵的距离。代码使用了while循环、max和min函数来处理输入数据。
摘要由CSDN通过智能技术生成

题目描述


题目思路

纯板子题,与Ants如出一辙。


我的代码

注意士兵数可能为0。

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	int l;
	int n;
	cin >> l >> n;
	int near = -1;
	int far = -1;
	if (n != 0) {
		while (n--) {
			int t;
			cin >> t;
			int t2 = l - t + 1;
			near = max(min(t, t2), near);
			far = max(max(t, t2), far);
		}
		cout << near << " " << far << endl;
	}
	else {
		cout << 0 << " " << 0;
	}

	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值