POJ 1064 Cable master

题目链接:

http://poj.org/problem?id=1064

解题思路:

用二分查找判断一个解是否可行。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#define eps 1e-9
#define INF 100005
using namespace std;

int n,k;
double a[10005];

bool check(double x){
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += (int)(a[i]/x);
    return sum >= k;
}

int main(){
    while(~scanf("%d%d",&n,&k)){
        for(int i = 0; i < n; i++)
            scanf("%lf",&a[i]);
        double l = 0, r= INF;
        while((r-l) >= eps){
        //for(int i = 0; i < 100; i++){
        //这两种循环均可以过
            double mid = (l+r)/2;
            if(check(mid))
                l = mid;
            else
                r = mid;
        }
        printf("%.2f\n",floor(r*100)/100);//坑人呀。。。写成lf就wrong了。。。必须要改成f
    }
    return 0;
}


你可能感兴趣的:(二分查找)