北大 算法基础 雷达安装问题POJ1328

练习代码:

#include<iostream> 
#include<algorithm>
using namespace std;
#define MAXS (10000)

int n, d;//小岛数量,半径
struct Line
{
	double l, r;
};
Line Lines[MAXS];
int solve() {
	int ans=1;//最少雷达数
	double nowR=Lines[0].r;
	for (int i = 1; i < n; i++)
		if (Lines[i].l <= nowR)//线段和上一条有没有重合
			nowR = min(nowR, Lines[i].r);
		else
		{
			ans++;
			nowR = Lines[i].r;
		}
	return ans;
}
bool operator<(Line l1, Line l2) {
	return l1.l < l2.l;
}

int main() {	
	int x, y;
	while (1)
	{
		int num=1;
		cout << "请输入小岛个数和半径:";
		cin >> n >> d;		
		if (n == 0 && d == 0)
			break;
		else
		{
			cout << "请输入每个小岛的坐标:";
			for (int i = 0; i < n; i++)
			{
				cin >> x >> y;
				Lines[i].l = x - sqrt(d*d - y * y);
				Lines[i].r = x + sqrt(d*d - y * y);				
			}
			sort(Lines, Lines + n);
			cout << "Case" << num << ":" << solve() << endl;
		}
		cout << endl;
		num++;
	}

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值