本帖最后由 yuxijian2020 于 2021-4-17 21:18 编辑 #include <iostream>
#include <vector>
using namespace std;
struct Cicle
{
int x;
int y;
int r;
Cicle(int x, int y, int r) : x(x), y(y), r(r) {}
};
int CalcArea(vector<Cicle>& trees)
{
for (size_t i = 0; i < trees.size(); ++i)
{
for (size_t j = i + 1; j < trees.size(); ++j)
{
//判断圆是否重叠
if ((trees[i].x - trees[j].x) * (trees[i].x - trees[j].x)
+ (trees[i].y - trees[j].y) * (trees[i].y - trees[j].y)
<= (trees[i].r + trees[j].r) * (trees[i].r + trees[j].r))
trees.erase(trees.begin() + j);
}
}
int result = 0;
for (size_t i = 0; i < trees.size(); ++i)
{
result += trees[i].r * trees[i].r;
}
return result;
}
void GetInput()
{
int number = 0;//总人数
vector<Cicle> trees;//所有树信息的数组
int tempX = 0, tempY = 0, tempR = 0;//用于输入
printf_s("请输入总人数: ");
cin >> number;
for (int i = 0; i < number; ++i)
{
printf_s("请输入第 %d 个树的坐标和半径(用空格隔开): ", i + 1);
scanf_s("%d%*c%d%*c%d", &tempX, &tempY, &tempR);
trees.emplace_back(Cicle(tempX, tempY, tempR));
}
printf_s("不重叠的最大面积为: %d\n", CalcArea(trees));
}
int main()
{
GetInput();
return 0;
}
|