请用c语言实现 描述 Aerse has got N ropes,the length of them is an array L[i](i = 0,1,2,…,N-1)。Aerse uses a knife full of DUANG power to cut them into M ropes which have the same length in one cut(you cannot tie short ropes). Here is the question, what is the maximum length of the M ropes? 输入 There are multiple test cases. For every test case, first line has two integers N and M(1 <= N,M < 10000). The following N line is the N number of length of the ropes L[i](0 < L[i] < 1000). 输出 For every testcase, output the maximum length as mentioned above(round to the nearest tenth).
时间: 2023-06-12 09:08:27 浏览: 83
```c
#include <stdio.h>
#include <math.h>
#define MAX_N 10000
int n, m;
double ropes[MAX_N];
int check(double len) {
int cnt = 0;
for (int i = 0; i < n; i++) {
cnt += (int)(ropes[i] / len);
if (cnt >= m) return 1;
}
return 0;
}
int main() {
while (scanf("%d%d", &n, &m) != EOF) {
double max_len = 0;
for (int i = 0; i < n; i++) {
scanf("%lf", &ropes[i]);
if (ropes[i] > max_len) max_len = ropes[i];
}
double l = 0, r = max_len;
while (r - l >= 1e-5) {
double mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%.1lf\n", floor(r * 10 + 0.5) / 10);
}
return 0;
}
```
首先输入数据,然后确定二分答案的左右边界,左边界为0,右边界为所有绳子中最长的那个。然后在这个区间里进行二分,每次判断能不能用长度为mid的绳子来切分出m条一样长的绳子。如果可以,就将左边界移动到mid,否则将右边界移动到mid。最后输出右边界,即为答案,注意四舍五入。