【正经题解】洒洒水
2024-02-22 14:39:24
发布于:浙江
14阅读
0回复
0点赞
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
// 计算喷水装置能覆盖的三角形底边长度
float calculateTriangleBase(float radius, float width) {
return 2 * sqrt(radius * radius - width * width);
}
// 比较函数,用于排序
bool compare(float a, float b) {
return a > b;
}
int main() {
int testCases, numberOfSprinklers;
float sprinklerRadius[601], totalLength, temp;
cin >> testCases;
while (testCases--) {
totalLength = 0.0;
cin >> numberOfSprinklers;
// 读取每个喷水装置的半径
for (int i = 0; i < numberOfSprinklers; i++) {
scanf("%f", &sprinklerRadius[i]);
}
// 将喷水装置半径降序排序
sort(sprinklerRadius, sprinklerRadius + numberOfSprinklers, compare);
for (int i = 0; i < numberOfSprinklers; i++) {
totalLength += calculateTriangleBase(sprinklerRadius[i], 1.0);
// 判断是否已经覆盖了整个农场
if (totalLength >= 20) {
cout << i + 1 << endl;
break;
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个