題意:有一些棍子,這些棍子的長度已知,現在要將這些棍子分成m段,問分的棍子最長是多少。
思路:二分枚舉答案,注意精度控制。浮點數的二分和整數的二分還不太一樣,需要注意一下。
代碼:
[cpp]
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
double eps = 1e-5;
const int N = 10010;
double num[N];
int main(){
//freopen("1.txt","r",stdin);
int n,k;
while(scanf("%d%d",&n,&k) != EOF){
double maxvalue = 0.0;
for(int i = 0; i < n; ++i){
scanf("%lf",&num[i]);
if(maxvalue < num[i])
maxvalue = num[i];
}
double lp = 0.00,rp = maxvalue,ans = 0.0;
while(rp - lp > eps){
double mid = (rp + lp) / 2.0;
int sum = 0;
for(int i = 0; i < n; ++i){
int x = (num[i] / mid );
sum += x;
}
if(sum >= k){
ans = mid;
lp = mid;
}
else{
rp = mid;
}
}
rp = rp * 100;
int yy = rp;
printf("%.2lf\n",yy * 0.01);
}
return 0;
}