练习代码:
#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;
}