POJ 1064 Cable master 浮点数二分
题意:有一些棍子,这些棍子的长度已知,现在要将这些棍子分成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;
}
补充:软件开发 , C++ ,