poj 1064 Cable master(二分)

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

书上例题,还有个重要的是浮点数的表示是四舍五入的,这个以前不知道

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
int k;
double t[10000];
bool f(double l)
{
    int s=0;
    for(int i=0; i<n; i++)
    {
        double c=t[i]; //开始错了
        while(c>=l)
        {
            c-=l;
            s++;
        }
    }
    if(s>=k)return true;
    else return false;
}
int main()
{
    /*
    int a=399;
    double b=3.999;
    printf("%.2f\n",a*1.0/100);
    printf("%.2f\n",b*100/100);
    printf("%.2f\n",b/100);
     */
    scanf("%d%d",&n,&k);
    for(int i=0; i<n; i++)
    {
        scanf("%lf",&t[i]);
    }
    double p,q,m;
    int k;

    for(p=0,q=1e5,k=0; p<=q&&k<=100; k++)
    {
        m=(p+q)/2;
        if(f(m)) p=m;
        else q=m;
        if(m<0.01)
        {
            m=0;
            break;
        }
    }
     printf("%.2f\n",int(m*100)*1.0/100);
    return 0;
}

 

posted @ 2016-03-19 19:45  cherry_yue  阅读(66)  评论(0)    收藏  举报